I do an example:
This is the code without problems:
int main(int argc, char **argv)
{
const char * commandsForGnuplot[] = {"set title \"A\"","plot [0:3][0:10] 'data1.temp' with linespoints lc rgb 'red'"};
double x1vals[NUM_POINTS] = {0.2 , 0.4 , 0.6 , 0.8, 1.0};
double y1vals[NUM_POINTS] = {1.0 , 1.0 , 1.0 , 1.0 , 1.0};
FILE * temp1 = fopen("data1.temp", "w");
/*Opens an interface that one can use to send commands as if they were typing into the
* gnuplot command line. "The -persistent" keeps the plot open even after your
* C program terminates.
*/
FILE * gnuplotPipe = popen ("gnuplot -persistent", "w");
int i;
for (i=0; i < NUM_POINTS; i++)
{
fprintf(temp1, "%lf %lf \n", x1vals[i] , y1vals[i]); //Write the data to a temporary file
}
for (i=0; i < NUM_COMMANDS; i++)
{
fprintf(gnuplotPipe, "%s \n", commandsForGnuplot[i]); //Send commands to gnuplot one by one.
}
return 0;
}
If i add a line to plot immediately the points instead of wait for the end of the execution i have this warning:
int main(int argc, char **argv)
{
const char * commandsForGnuplot[] = {"set title \"A\"","plot [0:3][0:10] 'data1.temp' with linespoints lc rgb 'red'"};
double x1vals[NUM_POINTS] = {0.2 , 0.4 , 0.6 , 0.8, 1.0};
double y1vals[NUM_POINTS] = {1.0 , 1.0 , 1.0 , 1.0 , 1.0};
FILE * temp1 = fopen("data1.temp", "w");
/*Opens an interface that one can use to send commands as if they were typing into the
* gnuplot command line. "The -persistent" keeps the plot open even after your
* C program terminates.
*/
FILE * gnuplotPipe = popen ("gnuplot -persistent", "w");
int i;
for (i=0; i < NUM_POINTS; i++)
{
fprintf(temp1, "%lf %lf \n", x1vals[i] , y1vals[i]); //Write the data to a temporary file
}
for (i=0; i < NUM_COMMANDS; i++)
{
fprintf(gnuplotPipe, "%s \n", commandsForGnuplot[i]); //Send commands to gnuplot one by one.
fflush(gnuplotPipe);
}
for (int i=0;i<10;i++)
{
std::cout<<i<<std::endl;
sleep(1.0);
}
return 0;
}
WARNING:
plot [0:3][0:10] 'data1.temp' with linespoints lc rgb 'red'
line 0: warning: Skipping data file with no valid points
What does it means??????