c - 如何在 c 中向 gnuplot 传递矩阵?

标签 c matrix pipe gnuplot

我使用管道直接在 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/

相关文章:

matlab - MATLAB 中的矩阵索引错误

matlab - 是否有一种方法可以从每个项目的多个实例矩阵中找到最少的唯一实体?

c - 找出进程数

c - 如何使用c程序在多个终端窗口中输出

c - C中的动态字符串数组结构

c - 二维莫顿解码功能 64bits

c++ - 在 Visual C++ 和 Python 之间交换数据

c - 当用户插入文本时,如何循环选项变量为int的菜单?

excel - excel中vba矩阵的语法

linux - & 和 | 有什么区别在Linux中?