如何将命令行参数传递给 gnuplot? [英] How to pass command line argument to gnuplot?
问题描述
我想使用 gnuplot 从数据文件中绘制图形,比如 foo.data.目前,我在命令文件中硬编码了数据文件名,比如 foo.plt,然后运行命令 gnuplot foo.plg
来绘制数据.但是,我想将数据文件名作为命令参数传递,例如运行命令 gnuplot foo.plg foo.data
.如何解析 gnuplot 脚本文件中的命令行参数?谢谢.
I want to use gnuplot to draw figure from data file, say foo.data. Currently, I hardcoded the data file name in the command file, say foo.plt, and run command gnuplot foo.plg
to plot data. However, I want to pass the data file name as a command argument, e.g. running command gnuplot foo.plg foo.data
. How to parse the command line arguments in gnuplot script file? Thanks.
推荐答案
可以通过开关-e
$ gnuplot -e "filename='foo.data'" foo.plg
在 foo.plg 中你可以使用那个变量
In foo.plg you can then use that variable
$ cat foo.plg
plot filename
pause -1
要使foo.plg"更通用,请使用条件:
To make "foo.plg" a bit more generic, use a conditional:
if (!exists("filename")) filename='default.dat'
plot filename
pause -1
注意 -e
必须在文件名之前,否则文件在 -e
语句之前运行.特别是,使用 ./foo.plg -e ...
CLI 参数运行 shebang gnuplot #!/usr/bin/env gnuplot
将忽略使用提供的参数.
Note that -e
has to precede the filename otherwise the file runs before the -e
statements. In particular, running a shebang gnuplot #!/usr/bin/env gnuplot
with ./foo.plg -e ...
CLI arguments will ignore use the arguments provided.
这篇关于如何将命令行参数传递给 gnuplot?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!