這個問題(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