使用system()与Gnuplot进行绘图会导致错误:文件不可读

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

本文关键字:错误 文件 绘图 system Gnuplot 使用      更新时间:2023-10-16

我试图通过system()在gnu图中显示我的数据。我在文件中写入一些示例值,然后尝试用gnuplot显示它们。

这就是我编写测试数据的方式:

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();
}

然后在主窗口中,我触发将数据写入文件,并调用Gnuplot显示图形:

void MainWindow::saveDataToFile(){
    myData->writeDataSetToFile();
}
void MainWindow::showGraph() {
    system("/usr/local/bin/gnuplot '/Users/rogerwilco/Desktop/test/plotter'");
}

我收到这个错误消息:

"/Users/rogerwilco/Desktop/test/plotter",第22行:警告:跳过不可读的文件"data.txt"/Users/rogerwilco/Desktop/test/ploter",第二十二行:绘图中没有数据

gnuplot的脚本如下:

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

带有错误的第22行是最后一行。然而,如果我自己使用的外壳

gnuplot 'plotter'

它是有效的。

为什么当我手动将命令输入到终端时它能工作,而当我通过system()输入命令时却不能工作?

系统:

  • 问题5.3.2
  • Mac OS X 10.9.5
  • gnuplot 4.6.6通过自制程序
  • AquaTerm 1.1.1通过自制程序clang 64位

我需要在最后一行添加整个路径:

CCD_ 1。

(应该感谢Galik和Chris Stratton让我走上了正确的道路)