I'm trying to figure out a way to speed up my code, any comments are welcome. Currently I write to file called data.dat and then read from it for plotting, but it is slow.
This program requires gnuplot or more specifically pgnuplot. You will need to
change the following line to the location of your pgnuplot within the code in order for the program to launch the plotter.
fpPlot = _popen("c:\\progra~1\\gnuoct~1.73\\bin\\pgnuplot"
Code:
#include <stdio.h>
#include <windows.h> // required for "#include <winbase.h>"
#include <winbase.h> // for sleep()
#define MAX_NUM 20
int main(void) {
int k;
FILE *fpPlot,
*fpWrite;
fpWrite = fopen("data.dat", "a");
if (fpWrite == NULL) {
printf("\nCANNOT CREATE OUTPUT FILE\nPROGRAM EXECUTION ABORTED.\n");
exit(1);
}
fprintf(fpWrite, "# Test file for C - gnuplot\n");
fclose(fpWrite);
fpPlot = _popen("c:\\progra~1\\gnuoct~1.73\\bin\\pgnuplot", "w");
fprintf(fpPlot, "set xrange[0:%d]\n", MAX_NUM);
fprintf(fpPlot, "set yrange[0:%d]\n", MAX_NUM);
fprintf(fpPlot, "set nokey\n");
for(k=0;k<MAX_NUM+1;k++) {
fpWrite = fopen("data.dat", "a");
if (fpWrite == NULL) {
printf("\nCANNOT CREATE OUTPUT FILE\nPROGRAM EXECUTION ABORTED.\n");
exit(1);
}
fprintf(fpWrite, "%d\n", k);
fflush(fpWrite);
fprintf(fpPlot, "plot \"data.dat\" with lines\n");
fflush(fpPlot);
Sleep(500);
fclose(fpWrite);
}
Sleep(5000);
fclose(fpPlot);
fclose(fpWrite);
fprintf(fpPlot, "exit");
return 0;
}