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. ?

5

7 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); 
5

I've been using PLPlot for plotting from C and have found it both effective and easy. It's cross platform, open source, and supports a rich array of plot capabilities. I'd recommend having a look at the examples to get started.

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.

I have never used gnuplot, but if you look at this page () 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:

pbPlots is very easy to use and works with all C compilers.

Download pbPlots.c/h and supportLib.c/h from the github page and include them in your build.

Here is a complete example that will produce a plot in a PNG-file.

#include "pbPlots.h" #include "supportLib.h" int main(){ double x [] = {-1, 0, 1, 2, 3, 4, 5, 6}; double y [] = {-5, -4, -3, -2, 1, 0, 1, 2}; RGBABitmapImageReference *imageRef = CreateRGBABitmapImageReference(); DrawScatterPlot(imageRef, 600, 400, x, 8, y, 8); size_t length; double *pngData = ConvertToPNG(&length, imageRef->image); WriteToFile(pngData, length, "plot.png"); return 0; } 

the generated plot

There are a lot more options available, these are described on the github page.

4

There is a extensive library called DISLIN for scientific purpose. Avaliable even in Fortran language.

You can check examples in official website.

You can obtain it freely in DISLIN Homepage

2

I had a wee look around to see what other people have done regarding real-time plotting in gnuplot, and found this:

There's a little Perl script that can drive the plotting, and you just pipe your information in.

Since your data is being written to file, you may want to tail -f yourdata.dat and pipe that into the real-time plotter.

Also, because you're using the stdio file calls, you'd need to flush regularly (by calling fflush)

Obviously your simulation would be running in the background or in another shell. That way you could break out of the plotting at any time without interrupting the simulation.

Hope that's of some use to you.

2

Step by step solution to plot using C programming language:

Step 1 requirements: 1. Windows 10/11 64bit 2. Install eclipse IDE (CDT or scientific computing) 3.Install MSYS2 4. Install mingw64 required component for c compiler using MSYS2 and adding it's binary path to windows environment variable 5. Install mathgl library using MSYS2: Installing mathgl

Step 2 Crete eclipse project witch includes required mathgl required library. Create library Add required library to project:

Right click on the project -> Expand C/C++ Build -> Setting -> Expand MinGW C Linker (if wasn't expanded) -> Libraries -> Libraries (-l) -> add...

Here you must add mgl2 and mgl2-fltk: library inclution Step 3 (final) Writing a code to plot: create a .c file under project and write the below code to it.

#include <mgl2/mgl_cf.h> //#include <mgl2/wnd_cf.h> #include <mgl2/fltk.h> #include <synchapi.h> HMGL gr = NULL;//HMGL mgl_create_graph_fltk int main(int argc,char **argv) { gr = mgl_create_graph_fltk(NULL, "First C graph", NULL, NULL); double x[50]={0}; HMDT dat=mgl_create_data_size(50, 1, 1); while(1) { Sleep(100); mgl_clf ( gr) ; mgl_set_range_val(gr, 'y', -1, 1); mgl_set_range_val(gr, 'x', 0, 512); mgl_set_origin(gr, 0, 0, 0); // mgl_box(gr); mgl_axis(gr, "", "", ""); mgl_set_font_size(gr, 1.5); // mgl_grid(gr, a, "", ""); int i=0; while(i<50) { x[i]=(mgl_rnd()*2)-1; i++; } mgl_data_set_double(dat, x, 50, 1, 1); // mgl_line(gr,0,0,0,2*mgl_rnd()-1,2*mgl_rnd()-1,0,"Ar2",-.5); mgl_stem(gr, dat, "ro", ""); // mgl_wnd_update(gr); } return 0; } 

After building this code, you will find an executable included in the debug folder.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.