2

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
romeovs
  • 5,785
  • 9
  • 43
  • 74

1 Answers1

1

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")
Erik
  • 88,732
  • 13
  • 198
  • 189
  • tried that, but it doesn't work. Gnuplot seems hell-bent on displaying every signle iteration in the fitting process – romeovs Mar 08 '11 at 15:44
  • 1
    It just came to me: using `2>`in stead of `>`it works! apparently the `fit`iterations are error outputs.. Edit your post and I'll accept. – romeovs Mar 08 '11 at 15:45
  • 1
    Note: In windows `/dev/null` needs to be `/nul`. – HeyYO Apr 04 '13 at 11:05