这个问题(gnuplot store one number from data file into variable)对我有一些提示的第一个答案。
在我的情况下,我有一个包含抛物线参数的文件。我已经将参数保存在gnuplot变量中。然后我绘制每个时间步包含参数变量的函数。
#!/usr/bin/gnuplot
datafile = "parabola.txt"
set terminal pngcairo size 1000,500
set xrange [-100:100]
set yrange [-100:100]
titletext(timepar, apar, cpar) = sprintf("In timestep %d we have parameter a = %f, parameter c = %f", timepar, apar, cpar)
do for [step=1:400] {
set output sprintf("parabola%04d.png", step)
# read parameters from file, where the first line is the header, thus the +1
a=system("awk '{ if (NR == " . step . "+1) printf \"%f\", $1}' " . datafile)
c=system("awk '{ if (NR == " . step . "+1) printf \"%f\", $2}' " . datafile)
# convert parameters to numeric format
a=a+0.
c=c+0.
set title titletext(step, a, c)
plot c+a*x**2
}
这给出了一个系列叫parabola0001.png, parabola0002.png PNG文件, parabola0003.png, ...,分别表示从被称为parabola.txt
文件中读取参数抛物线。标题包含给定时间步的参数。
对于了解你要知道的是,gnuplot的system()
功能:
- 双引号里面的东西不被gnuplot的
- 点解析是在gnuplot的连接字符串
- 为双引号AWK
printf
命令已经被越狱,从gnuplot的解析器隐藏
为了测试这个gnuplot的脚本,保存成任意名称的文件,例如parabolaplot.gplot
并使其可执行(chmad a+x parabolaplot.gplot
)。该parabola.txt
文件可以用
awk 'BEGIN {for (i=1; i<=1000; i++) printf "%f\t%f\n", i/200, i/100}' > parabola.txt
您为什么在寻找单线解决方案? – mgilson 2013-02-21 17:31:41
当我使用'gnuplot'时,我总是从'Perl'中调用它。如果这不是一个选项,我会建议从'gnuplot'脚本内部调用'awk'。见[这里](http://stackoverflow.com/questions/12846717/using-awk-or-other-shell-command-inside-gnuplot-function)和[here](http://security.riit.tsinghua。 edu.cn/~bhyang/ref/gnuplot/datafile3-e.html)来实现这个目标。 – Steve 2013-02-21 23:49:51