Gnuplot, C ++ from windows. command line window opens and closes

I have the following, and no matter what I try, the command window opens and closes again. No graphs are displayed, files are not recorded. Anyone who has a solution to use gnuplot from C ++. I have both 4.4 and 4.6rc1.

#ifdef WIN32 gp = _popen("C:\Program Files (x86)\gnuplot\bin\pgnuplot.exe", "w"); #else gp = popen("gnuplot -persist", "w"); #endif if (gp == NULL) return -1; /* fprintf(gp, "unset border\n"); fprintf(gp, "set clip\n"); fprintf(gp, "set polar\n"); fprintf(gp, "set xtics axis nomirror\n"); fprintf(gp, "set ytics axis nomirror\n"); fprintf(gp, "unset rtics\n"); fprintf(gp, "set samples 160\n"); fprintf(gp, "set zeroaxis"); fprintf(gp, " set trange [0:2*pi]");*/ fprintf(gp, "set term png\n"); fprintf(gp, "set output \"c:\\printme.png\""); fprintf(gp, "plot .5,1,1.5\n"); fprintf(gp, "pause -1\n"); fflush(gp); 
+4
source share
2 answers

The following program was tested on Windows using Visual Studio and MinGW compilers, and on GNU / Linux using gcc . The gnuplot binary must be in the path, and Windows must use the pgnuplot enabled pgnuplot .

I found that Windows feeds are much slower than the corresponding ones in GNU / Linux. For large datasets, transferring data to gnuplot through a pipe in Windows is slow and often unreliable. Moreover, the keypress wait code is more useful for GNU / Linux, where the graph window closes after pclose() called.

 #include <iostream> #include <stdio.h> #include <stdlib.h> // Tested on: // 1. Visual Studio 2012 on Windows // 2. Mingw gcc 4.7.1 on Windows // 3. gcc 4.6.3 on GNU/Linux // Note that gnuplot binary must be on the path // and on Windows we need to use the piped version of gnuplot #ifdef WIN32 #define GNUPLOT_NAME "pgnuplot -persist" #else #define GNUPLOT_NAME "gnuplot" #endif int main() { #ifdef WIN32 FILE *pipe = _popen(GNUPLOT_NAME, "w"); #else FILE *pipe = popen(GNUPLOT_NAME, "w"); #endif if (pipe != NULL) { fprintf(pipe, "set term wx\n"); // set the terminal fprintf(pipe, "plot '-' with lines\n"); // plot type for(int i = 0; i < 10; i++) // loop over the data [0,...,9] fprintf(pipe, "%d\n", i); // data terminated with \n fprintf(pipe, "%s\n", "e"); // termination character fflush(pipe); // flush the pipe // wait for key press std::cin.clear(); std::cin.ignore(std::cin.rdbuf()->in_avail()); std::cin.get(); #ifdef WIN32 _pclose(pipe); #else pclose(pipe); #endif } else std::cout << "Could not open pipe" << std::endl; return 0; } 
+5
source

Of course, the following answer is very similar to the answer of Nicholas Kinar, the only point added is how to save the .png file correctly. Also a few suggestions:

  • If the input path has spaces, it will not work. On Windows 8 using Visual Studio 2013, the error message "C:Program" is not recognized as an internal or external command....
  • If you do not specify an output path, it prints and stores in the directory of the C ++ program itself. Therefore, you should check the correct output path format, since gnuplot does not show any errors, it is difficult to determine the exact cause.

Below is the complete C ++ program that works fine on Visual Studio 2013 on Windows 8.1.

 #include <cstdio> #include <iostream> int main() { FILE* pipe = _popen("C:/gnuplot/bin/pgnuplot.exe", "w"); if (pipe != NULL) { fprintf(pipe, "set term win\n"); fprintf(pipe, "plot(x, sin(x))\n"); //a simple example function fprintf(pipe, "set term pngcairo\n"); fprintf(pipe, "set output \"myFile.png\"\n" ); fprintf(pipe, "replot\n"); fprintf(pipe, "set term win\n"); fflush(pipe); } else puts("Could not open the file\n"); _pclose(pipe); //system("pause"); return 0; } 
+2
source

Source: https://habr.com/ru/post/1397286/


All Articles