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");
fflush(gnuplotPipe);
fprintf(stderr,"Press enter to continue...");
fflush(stderr);
getchar();
// exit gnuplot
fprintf(gnuplotPipe,"exit \n");
pclose(gnuplotPipe);
}
Cite as:
Saad, T. "How to Call Gnuplot from c Using Pipes (Windows)".
Weblog entry from
Please Make A Note.
http://pleasemakeanote.blogspot.com/2009/05/how-to-call-gnuplot-from-c-using-pipes.html
#include
ReplyDeletevoid 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 );
fflush(p);
fflush(stderr);
getchar();
fprintf(p,"exit \n");
_pclose(p);
}
}
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.
ReplyDeleteThis 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.
ReplyDeletedoesn'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
ReplyDelete