来自C ++定义变量的C ++ Gnuplot管道输入 [英] C++ Gnuplot pipe input from C++ defined variables

查看:76
本文介绍了来自C ++定义变量的C ++ Gnuplot管道输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C ++通过以下代码将命令传递给gnuplot:

I am using C++ to pipe commands to gnuplot using the following code:

FILE *gnuplotPipe = popen("gnuplot -persist", "w");  // Open a pipe to gnuplot

if (gnuplotPipe) {   // If gnuplot is found

  fprintf(gnuplotPipe, "reset\n"); //gnuplot commands
  fprintf(gnuplotPipe, "n='500'\n");
  fprintf(gnuplotPipe, "max='1500'\n");
  fprintf(gnuplotPipe, "min='-1500\n");
  fprintf(gnuplotPipe, "width=(max-min)/n\n");
  fprintf(gnuplotPipe, "hist(x,width)=width*floor(x/width)+width/2.0\n");
  fprintf(gnuplotPipe, "set term png #output terminal and file\n");
  fprintf(gnuplotPipe, "set output 'Observable_Histogram.png'\n");
  fprintf(gnuplotPipe, "set xrange [min:max]\n");
  fprintf(gnuplotPipe, "set yrange [0:]\n");
  fprintf(gnuplotPipe, "set offset graph 0.05,0.05,0.05,0.0\n");
  fprintf(gnuplotPipe, "set xtics min,(max-min)/5,max\n");
  fprintf(gnuplotPipe, "set boxwidth width*0.9\n");
  fprintf(gnuplotPipe, "set style fill solid 0.5\n");
  fprintf(gnuplotPipe, "set tics out nomirror\n");
  fprintf(gnuplotPipe, "set xlabel 'Observable'\n");
  fprintf(gnuplotPipe, "set ylabel 'Counts'\n");
  fprintf(gnuplotPipe, "set title 'Observable'\n");
  fprintf(gnuplotPipe, "plot 'output.txt' u (hist($1,width)):(1.0) smooth freq w boxes lc rgb'green' notitle\n");

  fflush(gnuplotPipe); //flush pipe

  fprintf(gnuplotPipe,"exit \n");   // exit gnuplot
  pclose(gnuplotPipe);    //close pipe

}

这很完美,但是我希望它能够从c ++中以前定义的变量中获取输入.
例如,不是直接定义n ='500',min ='-1500',max ='1500'等,我想使用代码的前面(从用户输入)已经定义的变量,即int n,int max,int min,字符串标题,字符串xlabel等.

This works perfectly, however I want it to be able to take input from previously defined variables in c++.
For example, instead of straight up defining n='500', min='-1500', max='1500', etc., I want to use variables that I already defined (from user input) earlier in the code, i.e. int n, int max, int min, string title, string xlabel, etc.

我已经尝试了所有我能想到的,例如:

I have tried everything I can think of, such as:

fprintf(gnuplotPipe, "max=");
fprintf(gnuplotPipe, 'max');

或:

fprintf(gnuplotPipe, "max=" 'max' "\n");

不幸的是没有任何事情.

and nothing works unfortunately.

有人对我如何使它起作用有任何想法吗?

Does anyone have any ideas on how I could get this working?

提前谢谢!

推荐答案

您想要做的正是fprintf()的用途,请参见

The thing you want to do is exactly what fprintf() is for, see the manual. Here is an example:

int maximum = 500; // taken from user input maybe
fprintf(gnuplotPipe, "max=%d\n", maximum);

您当前正在以更简单的fputs()的方式使用fprintf().

You're currently using fprintf() in a way of more simple fputs().

这篇关于来自C ++定义变量的C ++ Gnuplot管道输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆