Thursday, May 14, 2009

How to Call Gnuplot from c Using Pipes (Windows)

Try this:

// Open a pipe to gnuplot
FILE *gnuplotPipe = popen("c:\\gnuplot\\bin\\pgnuplot -persist","w");
// If gnuplot is found
if (gnuplotPipe) {
// using fprintf on the gnuplotPipe, we can directly issue commands in gnuplot
fprintf(gnuplotPipe, "set style data lines\n");
fprintf(gnuplotPipe, "cd 'C:\\gnuplot\\bin'\n");
// assuming your data file is placed in the gnuplot bin directory
fprintf(gnuplotPipe, "plot \"data-file.dat\" using 1:2\n");
fprintf(stderr,"Press enter to continue...");
// exit gnuplot
fprintf(gnuplotPipe,"exit \n");

Cite as:
Saad, T. "How to Call Gnuplot from c Using Pipes (Windows)". Weblog entry from Please Make A Note.


  1. #include

    void main()
    FILE *p = _popen("c:\\gnuplot\\binary\\gnuplot -persist","w");
    float data[4] = { 0,0,.1,0 };
    if (p)
    fprintf(p, "set xrange [1:10]\n");
    fprintf(p, "set yrange [1:15]\n");

    fprintf(p, "plot '-' binary endian=little array=4 format=\"%%float\" with linespoints\r");
    fwrite(data, sizeof(data), 1, p );

    fprintf(p,"exit \n");

  2. I am new to using these pipe thing. Can anyone tell me why getchar() is needed? And how to force the command through the fprintf() to run immediately? To make things clear let me explain my situation. I output my data to a file, and use that file to plot with gnuplot. The output change in each loop, I want to use plot to refer to the latest data, but with above code, you will have to push enter key every time.I want it to run automatically until the end of the loop. Can anyone tell how to do this? Thanks.

  3. This is what I wanted to get because I like to improving my knowledge, actually there were a lot of things I didn't know and now I got those things that were worrying me.

  4. doesn't work in dev c, how can i put it to work ?? do i have to run in in cygwin because of the pipe thing?? thanks, i'm new at this :D