图自动这使得C code地块 [英] Making C code plot a graph automatically
问题描述
我已经写了一个程序,它用的,然后使用gnuplot的单独绘制它打算写入数据的列表为'.DAT'文件。有没有让我的code自动绘制它的一种方式?我的输出形式为:
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
....
在理想情况下,当我运行code曲线也将印有X标记,Y标签和标题(可从我的C code更改)。非常感谢。
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.
推荐答案
我碰到这个而前来寻找关于gnuplot的东西。尽管这是一个老问题,我想我会贡献一些示例code。我用这个我的一个程序,我想它做了pretty整洁的工作。据我所知,这PIPEing仅适用于Unix系统(参见下面的Windows用户编辑)。我gnuplot的安装是默认从Ubuntu库安装。
I came across this while searching for something else regarding gnuplot. Even though it's an old question, I thought I'd contribute some sample code. I use this for a program of mine, and I think it does a pretty tidy job. AFAIK, this PIPEing only works on Unix systems (see the edit below for Windows users). My gnuplot installation is the default install from the Ubuntu repository.
#include <stdlib.h>
#include <stdio.h>
#define NUM_POINTS 5
#define NUM_COMMANDS 2
int main()
{
char * commandsForGnuplot[] = {"set title \"TITLEEEEE\"", "plot 'data.temp'"};
double xvals[NUM_POINTS] = {1.0, 2.0, 3.0, 4.0, 5.0};
double yvals[NUM_POINTS] = {5.0 ,3.0, 1.0, 3.0, 5.0};
FILE * temp = fopen("data.temp", "w");
/*Opens an interface that one can use to send commands as if they were typing into the
* gnuplot command line. "The -persistent" keeps the plot open even after your
* C program terminates.
*/
FILE * gnuplotPipe = popen ("gnuplot -persistent", "w");
int i;
for (i=0; i < NUM_POINTS; i++)
{
fprintf(temp, "%lf %lf \n", xvals[i], yvals[i]); //Write the data to a temporary file
}
for (i=0; i < NUM_COMMANDS; i++)
{
fprintf(gnuplotPipe, "%s \n", commandsForGnuplot[i]); //Send commands to gnuplot one by one.
}
return 0;
}
修改
在我的申请,我也碰到了,该地块没有出现,直到调用程序被关闭的问题。为了解决这个问题,添加 fflush(gnuplotPipe)
你用过之后 fprintf中
来发送你的最后一个命令。
In my application, I also ran into the problem that the plot doesn't appear until the calling program is closed. To get around this, add a fflush(gnuplotPipe)
after you've used fprintf
to send it your final command.
我也看到,Windows用户可以使用 _popen
在的popen
的 - 不过,我可以' t请确认这个,因为我不已经安装了Windows。
I've also seen that Windows users may use _popen
in place of popen
-- however I can't confirm this as I don't have Windows installed.
编辑2
一个可以避免通过发送gnuplot的在情节写入文件 -
命令后面的数据点后面的字母E。
One can avoid having to write to a file by sending gnuplot the plot '-'
command followed by data points followed by the letter "e".
例如
fprintf(gnuplotPipe, "plot '-' \n");
int i;
for (int i = 0; i < NUM_POINTS; i++)
{
fprintf(gnuplotPipe, "%lf %lf\n", xvals[i], yvals[i]);
}
fprintf(gnuplotPipe, "e");
这篇关于图自动这使得C code地块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!