Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot data by c program?

Tags:

I am a mechanical engineer who has only limited knowledge in C programming. I wrote some code in order to make simulations, and I want to visualize the simulation results. At the moment I am using Dev-C for writing my codes. With fopen and fprintf commands I generate a .dat file which includes the results. Then I open GNUPLOT program and import my .dat file to plot the results. This takes time and I have to wait till the end of the simulation. Is there an easy way to connect my plotter with Dev-C, so my plotter starts plotting data during the simulation? Any library or etc. ?

like image 421
CrazyHorse Avatar asked Jan 14 '13 02:01

CrazyHorse


People also ask

How do you plot in C programming?

C and Gnuplot can be used to plot complex functions. One can write the function in C and then write the values of the function at various values in a txt file, which can then be plotted using Gnuplot. The txt file should have numerical values in at least two columns. The first column is for x values.


2 Answers

Since you already know gnuplot, the simplest thing to do may be to just call gnuplot from your program and pipe the data to it:

FILE *gnuplot = popen("gnuplot", "w"); fprintf(gnuplot, "plot '-'\n"); for (i = 0; i < count; i++)     fprintf(gnuplot, "%g %g\n", x[i], y[i]); fprintf(gnuplot, "e\n"); fflush(gnuplot); 
like image 82
Edgar Bonet Avatar answered Sep 19 '22 02:09

Edgar Bonet


OK, one solution, as you are writing out to a file, would be to just make a system() call when you write out to the file, and call gnuplot.

But, that means you should change the filename each time, but I fear if you do that that it won't look correct, since you are sending small amounts of data each time.

http://www.gnu.org/software/libc/manual/html_node/System-Calls.html

I have never used gnuplot, but if you look at this page (http://gnuplot-tricks.blogspot.com/) you may find some tricks that would enable this to work well.

But, I think, unless you are going to plot everything yourself, and skip gnuplot, then you may just need to wait, as you are.

You may find the C interface to gnuplot may help you:

http://ndevilla.free.fr/gnuplot/

like image 41
James Black Avatar answered Sep 22 '22 02:09

James Black