RogerWilco77
RogerWilco77

Reputation: 339

using system() to plot with Gnuplot causes error: unreadable file

I try to display my data in gnu plot via system(). I write some sample values into the file and then try to get them shown with gnuplot.

This is how I write the test data:

void Dataset::writeDataSetToFile(){
    QString filename="/Users/rogerwilco/Desktop/test/data.txt";
    QFile file(filename);
    if(file.open(QIODevice::WriteOnly))
    {
        QTextStream stream (&file);
        stream << "1" << endl << "9" << endl << "15"<< endl;

    }
    file.close();

}

Then in the Mainwindow I trigger the writing of the data to the file and call Gnuplot to show the graph:

void MainWindow::saveDataToFile(){
    myData->writeDataSetToFile();

}

void MainWindow::showGraph() {
    system("/usr/local/bin/gnuplot \'/Users/rogerwilco/Desktop/test/plotter\'");
}

I receive this error message:

"/Users/rogerwilco/Desktop/test/plotter", line 22: warning: Skipping unreadable file "data.txt" "/Users/rogerwilco/Desktop/test/plotter", line 22: No data in plot

The script for gnuplot looks like this:

reset
n=100 #number of intervals
max=100.0 #max value
min=0.0 #min value
width=(max-min)/n #interval width
#function used to map a value to the intervals
hist(x,width)=width*floor(x/width)+width/2.0
#set term png #output terminal and file
set output "histogram.png"
set xrange [min:max]
set yrange [0:]
#to put an empty boundary around the
#data inside an autoscaled graph.
set offset graph 0.05,0.05,0.05,0.0
set xtics min,(max-min)/5,max
set boxwidth width*0.9
set style fill solid 0.5 #fillstyle
set tics out nomirror
set xlabel "x"
set ylabel "Frequency"
#count and plot
plot "data.txt" u (hist($1,width)):(1.0) smooth freq w boxes lc rgb"green" notitle

line 22 with the error is the last line. However, if I use the shell myself with

gnuplot 'plotter'

it works.

Why does it work when I enter the command manually into the terminal, but not when I do it via system()?

system:

Upvotes: 1

Views: 590

Answers (1)

RogerWilco77
RogerWilco77

Reputation: 339

I needed to add the whole path in the last line:

plot "/Users/rogerwilco/Desktop/test/data.txt" u (hist($1,width)):(1.0) smooth freq w boxes lc rgb"green" notitle.

(credits should go to Galik and Chris Stratton for brining me on the right path)

Upvotes: 0

Related Questions