开发者

hide C++ Gnuplot pipe console output

In C++I'm currently using this bit of code to plot some data using gnuplot. Gnuplot's fit command, however, produces a lot of unwanted output on the command line console (which I use for outputting some other stuff too, throughout the rest of my program).

Since this output clutters up my console output, I would like to disable it. There should be 2 ways to do this:

  1. Hide output created by external programs using pipe in C++
  2. Telling gnuplot to be silent and not output that much

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

fprintf(pipe, "set terminal x11 enhanced\n"); //set appropriate output terminal for the plot 
fprintf(pipe, "set xlabel 'N'\n");//set xlabel
fprintf(pipe, "set ylabel 'error'\n");//set ylabel
fprintf(pipe, "set yrange [0:0.0001]\n");//set the y range
fprintf(pipe, "plot 'dataSimp.dat' ti 'Simpson method'\n");//plot the Simp error
fprintf(pipe, "replot 'dataTrap.dat' ti 'Trapezium method' \n");//plot the Trap error
fprintf(pipe, "replot 1/(x**2) ti '1/{x^2}'\n");//plot y=1/(x^2) function for comparison
fprintf(pipe, "replot 1/(x**4) ti '1/{x^4}'\n");//plot y=1/(x^4) function for comparison

//fit curve to dataSimp
fprintf(pipe, "set output 'rommel.txt'");
fprintf(pipe, "fSimp(x)=aSimp/(x**4) \n");
fprintf(pipe, "fit fSimp(x) 'dataSimp.dat' via 开发者_开发问答aSimp\n");
fprintf(pipe, "replot aSimp/(x**4) ti 'fitted aSimp/{x^4}'\n");

//fit curve to dataSimp
fprintf(pipe, "fTrap(x)=aTrap/(x**2) \n");
fprintf(pipe, "fit fTrap(x) 'dataTrap.dat' via aTrap \n");
fprintf(pipe, "replot aTrap/(x**2) ti 'fitted aTrap/{x^2}'\n");

pclose(pipe);//close gnuplot pipe


You could just use:

FILE *pipe = popen("gnuplot -persist > /dev/null", "w")

Or, if gnuplot uses stderr:

FILE *pipe = popen("gnuplot -persist > /dev/null 2>&1", "w")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜