I've been researching ways of visualizing data and stumbled upon gnuplot. It seems great and really useful but I was trying to find a way of embedding it within the program itself instead of having to feel the data separately. I then stumbled on a stack overflow answer where the topic was discussed but they gloss over how certain things work since it didn't pertain to their question at the moment so I'm asking here:
Their post here:
So the code extract would be:
void gnuprint(FILE *gp, double x[], int N){
int i;
fprintf(gp, "plot '-' with lines\n");
for (i=0; i<N; i++){fprintf(gp, "%g %g\n", x[i],WF[2*i+1]);}
fflush(gp);
fprintf(gp, "e\n");
}
From what I understand from the code they seem to have opened the gnuplot program as a file and piping in the information as if it was writing into a file. I simply fail to understand how one would initialize the gp pointer to hold the program open in such a state. Any help understanding how this is done would be appreciated. Thanks
The code extract you give for gnuprint() is incomplete or a bit mangled. It provides no definition for WF[]. Removing that and wrapping it in a minimal main.c gives the following simple example:
#include <stdio.h>
void gnuprint(FILE *, double *, int);
int main()
{
double x[7] = {11,55,22,44,33,77,66};
FILE *gp;
/* Open a pipe to gnuplot giving the 'persist' option */
gp = popen("gnuplot -p", "w");
gnuprint(gp, x, 7);
}
void gnuprint(FILE *gp, double x[], int N)
{
int i;
fprintf(gp, "plot '-' with lines\n");
for (i=0; i<N; i++){fprintf(gp, "%d %g\n", i, x[i]);}
fflush(gp);
fprintf(gp, "e\n");
}
This compiles, links, and builds to give an executable that invokes "gnuplot -p" and feeds it commands and data to plot. The "-p" option to gnuplot tells it to leave the plot showing on the screen after it exits.