gunplot demo
1 //author : Leon yangli0534@gmail.com 2 3 #include <stdlib.h> 4 #include <stdio.h> 5 #include <math.h> 6 #define NUM_POINTS 500 7 #define NUM_COMMANDS 2 8 9 int main() 10 { 11 char * commandsForGnuplot[] = {"set title \"gunplot demo\"", "plot 'data.temp'"}; 12 double fs = 1000000.0; 13 double f0 = 10000.0; 14 double xvals[NUM_POINTS] = {1.0, 2.0, 3.0, 4.0, 5.0}; 15 double yvals[NUM_POINTS] = {5.0 ,3.0, 1.0, 3.0, 5.0}; 16 FILE * temp = fopen("data.temp", "w"); 17 /*Opens an interface that one can use to send commands as if they were typing into the 18 * gnuplot command line. "The -persistent" keeps the plot open even after your 19 * C program terminates. 20 */ 21 FILE * gnuplotPipe = popen ("gnuplot -persistent", "w"); 22 int i; 23 for (i=0; i < NUM_POINTS; i++) 24 { 25 xvals[i] = i/fs; 26 yvals[i] = sin(2*M_PI*f0*xvals[i]); 27 } 28 for (i=0; i < NUM_POINTS; i++) 29 { 30 fprintf(temp, "%lf %lf \n", xvals[i], yvals[i]); //Write the data to a temporary file 31 } 32 33 for (i=0; i < NUM_COMMANDS; i++) 34 { 35 fprintf(gnuplotPipe, "%s \n", commandsForGnuplot[i]); //Send commands to gnuplot one by one. 36 } 37 return 0; 38 }
OPTIMISM, PASSION & HARDWORK