views:

64

answers:

1

I have written a program which writes a list of data to a '.dat' file with the intention of then plotting it separately using gnuplot. Is there a way of making my code plot it automatically? My output is of the form:

x-coord    analytic    approximation
x-coord    analytic    approximation
x-coord    analytic    approximation
x-coord    analytic    approximation
x-coord    analytic    approximation
 ....

Ideally, when I run the code the graph would also be printed with an x-label, y-label and title (which could be changed from my C code). Many thanks.

+2  A: 

You can either create a gnuplot script and spawn a process running gnuplot to plot this script from the commandline, or you may use one of the provided interfaces. For C, there is a POSIX pipe-based interface from Nicolas Devillard available here: http://ndevilla.free.fr/gnuplot/ ...and an iostream-based C++ version is available via git (see: http://www.stahlke.org/dan/gnuplot-iostream/ )

The most portable and probably the easiest way would still be calling gnuplot to plot a script, though. As sje397 mentioned, check your documentation for the system() call in stdlib.h.

On a sidenote, there is also GNU plotutils, which offers libplot, a library for plotting datasets, which you could use in your application. See: http://www.gnu.org/software/plotutils/

Jim Brissom
Im not fantastic at understand all this documentation stuff but I've found the declaration for system(): int system(const char *command); Is it then just a case of adding something like this?: system(gnuplot plot "data_set.dat" with 1:2 using lines);
Jack Medley
In the end I went for something like this:
Jack Medley
#include "gnuplot_i.h"int main(void){ FILE *outFile; outFile = fopen("Flight_Path.dat", "w"); /* Iterative loop which prints to output file: example.dat */ fclose(outFile); gnuplot_ctrl *k; k = gnuplot_init(); gnuplot_set_xlabel(k, "x"); gnuplot_set_ylabel(k, "y"); gnuplot_cmd(k,input_x); gnuplot_cmd(k,input_y); gnuplot_cmd(k, "set title \"Trajectory with Drag\""); gnuplot_cmd(k, "plot \"Flight_Path.dat\" using 1:2 title \"Flight Path\" with lines, \ sleep(7); gnuplot_close(k); return 0;}
Jack Medley
I'm sure you are missing some bits of that code, i.e. you are opening and closing the file immediately afterwards. Also, formatting in comments just sucks. Please edit your original post and add code to that - I would do this myself, but as I said, I believe your code is missing something.
Jim Brissom