获取从gnuplot到c++的变量

发布于 2025-01-22 11:54:13 字数 301 浏览 2 评论 0原文

我正在用C ++编写一个代码,该代码使用GNUPLOT绘制数据集,并且我意识到,如果我可以将变量从GNUPLOT获取到我的C ++代码,则可以简化代码。例如,如果我做了一个合适的f并获得他的统计信息

f(x)=a*x+b
fit f 'data.txt' via a,b
stats 'data.txt' u (f($1)):2 name 'Sf'

,即有任何方法可以获取sf_records并将其保存到我的代码中的变量中?喜欢int n = rf_records

感谢您的帮助

I'm writing a code in c++ which plots a dataset using gnuplot, and I realized that I could simplify my code a lot if I could get variables from gnuplot to my c++ code. e.g. if I did a fit f and get his stats i.e.

f(x)=a*x+b
fit f 'data.txt' via a,b
stats 'data.txt' u (f($1)):2 name 'Sf'

Is there any way to get, for example, the Sf_records and save it to a variable in my code? Like int N = Rf_records?

Thanks for your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最舍不得你 2025-01-29 11:54:13

假设您正在与gnuplot trougth a fifo进行通信,则可以订购gnuplot

print <variable>

,并且可以将其读取给您的c ++ program。

例如。要读取差异:

double getVal(std::string val){
  
  float ret;
  std::string str;
  
  str="print "; str+=val;str+="\n";
  
  fprintf(gp, str.c_str());
  fflush(gp);
  
  fscanf(gpin, "%f", &ret);
  
  return ret;
}

打开gnuplot fifo

void connecttognuplot(){
  
    //make an unique FIFO
    std::stringstream alma; alma.str(std::string()); alma<<std::clock(); GPFIFO="./gpio"+alma.str();
  
    //gnuplot => program FIFO
    if (mkfifo(GPFIFO.c_str(), 0600)) {
    if (errno != EEXIST) {
        perror(GPFIFO.c_str());
        unlink(GPFIFO.c_str());
    }
    }
    // For fprintf
    if (NULL == (gp = popen("gnuplot 2> /tmp/gp1","w"))) {
    perror("gnuplot");
    pclose(gp);
    }

    //  Redirect print
    fprintf(gp, "set print \"%s\";\n", GPFIFO.c_str());
    fflush(gp);

    // For fscanf
    if (NULL == (gpin = fopen(GPFIFO.c_str(),"r"))) {
    perror(GPFIFO.c_str());
    pclose(gp);
    }
  
  }

Assuming that you are communicating with gnuplot trougth a FIFO, you can order to gnuplot

print <variable>

and you can read this to your c++ program.

Eg. for reading a varirable:

double getVal(std::string val){
  
  float ret;
  std::string str;
  
  str="print "; str+=val;str+="\n";
  
  fprintf(gp, str.c_str());
  fflush(gp);
  
  fscanf(gpin, "%f", &ret);
  
  return ret;
}

To open a gnuplot FIFO

void connecttognuplot(){
  
    //make an unique FIFO
    std::stringstream alma; alma.str(std::string()); alma<<std::clock(); GPFIFO="./gpio"+alma.str();
  
    //gnuplot => program FIFO
    if (mkfifo(GPFIFO.c_str(), 0600)) {
    if (errno != EEXIST) {
        perror(GPFIFO.c_str());
        unlink(GPFIFO.c_str());
    }
    }
    // For fprintf
    if (NULL == (gp = popen("gnuplot 2> /tmp/gp1","w"))) {
    perror("gnuplot");
    pclose(gp);
    }

    //  Redirect print
    fprintf(gp, "set print \"%s\";\n", GPFIFO.c_str());
    fflush(gp);

    // For fscanf
    if (NULL == (gpin = fopen(GPFIFO.c_str(),"r"))) {
    perror(GPFIFO.c_str());
    pclose(gp);
    }
  
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文