живой сюжет на c ++ с использованием gnuPlot - PullRequest
0 голосов
/ 09 июля 2020

Я хочу построить данные на C ++, используя gnuplot. Для этого я разработал класс

gnuplot.hpp


#ifndef GNUPLOT_HPP
#define GNUPLOT_HPP

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cmath>
#include <vector>

template <class T>
class gnuplot
{
  public:
    gnuplot();

    ~gnuplot();

    void plot(const T off_A, const int& iterationNumber);

    static int plottingCall;
};

#endif

gnuplot.cc

#include "../includes/gnuplot.hpp"


template<class T>
int gnuplot<T>::plottingCall = 0;


template<class T>
gnuplot<T>::gnuplot()
{
  std::string gnuplot;
}


template<class T>
gnuplot<T>::~gnuplot()
{
   system("rm -f ./data/data.dat");
   system("rm -f ./data/plot.gnu");
}

template<class T>
void gnuplot<T>::plot(const T off_A, const int& iterationNumber)
{
   gnuplot<T>::plottingCall++;
   std::string dataFile = "./data/data.dat";
   std::string gnuplotFile = "./data/plot.gnu";

   std::ofstream dataFileStream(dataFile);
   std::ofstream gplotFileStream(gnuplotFile);
   
   dataFileStream.open(dataFile, std::ios::app);

   if (!dataFileStream.is_open() || !gplotFileStream.is_open()) 
   {
      std::cerr << "==> Gnuplot class, File(s) failed to open" << std::endl;
      exit(0);
   }

   dataFileStream << iterationNumber << " " << off_A << "\n" ;

   if (gnuplot<T>::plottingCall == 1)
   {
     std::cout << "==> plotting" << std::endl;
     std::ostringstream gnuplot;

     gnuplot << "set output './data/off_A.png'\n";
     gnuplot << "set terminal png\n";
     gnuplot << "set title \"off(A) vs. number of iteration in SYMMEIG algorithm\"\n";
     gnuplot << "set xlabel \"iteration number  \" \n";
     gnuplot << "set ylabel \"off(A) \" \n";
     gnuplot << "set terminal png size 1024,768\n";
     gnuplot << "set palette rgb 33,13,10\n";
     gnuplot << "plot \"" << dataFile << "\" u 1:2 with linespoint\n";
//     gnuplot << "set terminal x11\n";
//     gnuplot << "set output\n";
     gnuplot << "pause 1\n";
     gnuplot << "reread\n";

     std::string cmdStr = gnuplot.str();
     gplotFileStream << cmdStr;
     system("gnuplot ./data/plot.gnu");
   } 

   dataFileStream.close();
}

main.cc

#include "./includes/gnuplot.hpp"
#include "./src/gnuplot.cc"
#include <iostream>

int main()
{
  gnuplot<float> gplot = gnuplot<T>();

  for (int i = 0; i < 100; ++i)
  {
    gplot.plot(i*0.5,i);
  }
}

Но когда я компилирую и запускаю код, ничего не отображается, и data.dat также пуст. В чем источник проблемы в классе. Я думаю, что объект ofstream определен неправильно.

...