如何将命令行参数传递给gnuplot?

2024-06-25 23:17:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用gnuplot从数据文件中绘制图形,比如foo.data。目前,我在命令文件中硬编码了数据文件名,比如foo.plt,并运行命令gnuplot foo.plg来绘制数据。但是,我想将数据文件名作为命令参数传递,例如运行commandgnuplot foo.plg foo.data。如何解析gnuplot脚本文件中的命令行参数?谢谢。


Tags: 文件数据命令脚本图形编码datafoo
3条回答

您可以使用标记-c将参数传递给5.0版以后的gnuplot脚本。这些参数通过变量ARG0ARG9ARG0作为脚本和ARG1ARG9字符串变量访问。参数的数目由ARGC给出。

例如,下面的脚本(“script.gp”)

#!/usr/local/bin/gnuplot --persist

THIRD=ARG3
print "script name        : ", ARG0
print "first argument     : ", ARG1
print "third argument     : ", THIRD 
print "number of arguments: ", ARGC 

可以称为:

$ gnuplot -c script.gp one two three four five
script name        : script.gp
first argument     : one
third argument     : three
number of arguments: 5

或者在gnuplot内

gnuplot> call 'script.gp' one two three four five
script name        : script.gp
first argument     : one
third argument     : three
number of arguments: 5

在gnuplot 4.6.6和更早的版本中,存在一个call机制,其语法不同(现在已弃用)。参数通过$#$0,…,$9访问。例如,上面的相同脚本看起来像:

#!/usr/bin/gnuplot --persist

THIRD="$2"
print "first argument     : ", "$0"
print "second argument    : ", "$1"
print "third argument     : ", THIRD
print "number of arguments: ", "$#"

它在gnuplot中被称为(记住,version<;4.6.6)

gnuplot> call 'script4.gp' one two three four five
first argument     : one
second argument    : two
third argument     : three
number of arguments: 5

注意,脚本名没有变量,所以$0是第一个参数,变量在引号中调用。不能直接从命令行使用它,只能通过@con fu se建议的技巧。

您还可以按照建议通过环境传入信息here。这里重复了Ismail Amin的例子:

在外壳中:

export name=plot_data_file

在Gnuplot脚本中:

#! /usr/bin/gnuplot

name=system("echo $name")
set title name
plot name using ($16 * 8):20 with linespoints notitle
pause -1

您可以通过开关-e输入变量

$ gnuplot -e "filename='foo.data'" foo.plg

在foo.plg中,可以使用该变量

$ cat foo.plg 
plot filename
pause -1

要使“foo.plg”更通用一些,请使用条件:

if (!exists("filename")) filename='default.dat'
plot filename
pause -1

注意-e必须在文件名之前,否则文件将在-e语句之前运行。特别是,使用./foo.plg -e ...CLI参数运行shebang gnuplot#!/usr/bin/env gnuplot将忽略使用提供的参数。

相关问题 更多 >