我使用管道直接在 C 中调用 gnuplot。我使用以下命令打开管道:
#define GNUPLOT "gnuplot -persist"
FILE *gp;
gp = popen(GNUPLOT, "w");
现在,我想将一个矩阵(例如 zvalue[][]
)传递给 gnuplot,以便我可以使用以下命令来绘制 3D 图形:
fprintf(gp, "splot zvalue matrix using 1:2:3\n");
我在 C++ 中使用管道时成功做到了这一点:
#include "gnuplot-iostream.h"
需要使用-lboost_iostreams -lboost_system -lboost_filesystem
进行编译,它有一个函数gp.fileld(zvalue)
来处理这个问题,它是:
gp << "splot" << gp.file1d(zvalue) << "matrix using 1:2:3\n";
以便 gnuplot 可以访问矩阵 zvalue
。
我的问题是,是否有使用 C 管道的类似功能?
最佳答案
您必须将特殊文件名 -
与 splot
一起使用,然后将矩阵数据写入管道:
#include <stdio.h>
#define GNUPLOT "gnuplot -persist"
int main(void)
{
FILE *gp = popen(GNUPLOT, "w");
fprintf(gp, "splot '-' matrix using 1:2:3\n");
int i, j;
for (i = 0; i < 10; i++) {
for (j = 0; j < 10; j++)
fprintf(gp, "%d ", i*j);
fprintf(gp, "\n");
}
pclose(gp);
return 0;
}
当然,在您的情况下,您必须编写 fprintf(gp, "%e ", zvalue[i][j])
或类似内容。
关于c - 如何在 c 中向 gnuplot 传递矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29496878/