Yun*_*ang 138 command-line gnuplot
我想用gnuplot从数据文件中绘制图形,比如说foo.data.目前,我在命令文件中硬编码了数据文件名,比如foo.plt,并运行命令gnuplot foo.plg
来绘制数据.但是,我想将数据文件名作为命令参数传递,例如运行命令gnuplot foo.plg foo.data
.如何解析gnuplot脚本文件中的命令行参数?谢谢.
小智 180
您可以通过开关输入变量 -e
$ gnuplot -e "filename='foo.data'" foo.plg
Run Code Online (Sandbox Code Playgroud)
在foo.plg中,您可以使用该变量
$ cat foo.plg
plot filename
pause -1
Run Code Online (Sandbox Code Playgroud)
要使"foo.plg"更通用,请使用条件:
if (!exists("filename")) filename='default.dat'
plot filename
pause -1
Run Code Online (Sandbox Code Playgroud)
vag*_*rto 49
您可以使用标志将版本5.0以来的参数传递给gnuplot脚本-c
.这些参数是通过变量访问ARG0
到ARG9
,ARG0
作为脚本,并ARG1
以ARG9
字符串变量.参数的数量由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
Run Code Online (Sandbox Code Playgroud)
可以称为:
$ gnuplot -c script.gp one two three four five
script name : script.gp
first argument : one
third argument : three
number of arguments: 5
Run Code Online (Sandbox Code Playgroud)
或者在gnuplot中
gnuplot> call 'script.gp' one two three four five
script name : script.gp
first argument : one
third argument : three
number of arguments: 5
Run Code Online (Sandbox Code Playgroud)
在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: ", "$#"
Run Code Online (Sandbox Code Playgroud)
它在gnuplot中被称为(记住,版本<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
Run Code Online (Sandbox Code Playgroud)
请注意,脚本名称没有变量,$0
第一个参数也是如此,变量在引号内调用.没有办法直接从命令行使用它,只能通过@ con-fu-se建议的技巧.
Tho*_*hor 26
您还可以按照此处的建议通过环境传递信息.Ismail Amin的例子在这里重复:
在shell中:
export name=plot_data_file
Run Code Online (Sandbox Code Playgroud)
在Gnuplot脚本中:
#! /usr/bin/gnuplot
name=system("echo $name")
set title name
plot name using ($16 * 8):20 with linespoints notitle
pause -1
Run Code Online (Sandbox Code Playgroud)
Mo3*_*ius 23
Jari Laamanen的答案是最好的解决方案.我想解释一下如何在shell变量中使用多个输入参数:
output=test1.png
data=foo.data
gnuplot -e "datafile='${data}'; outputname='${output}'" foo.plg
Run Code Online (Sandbox Code Playgroud)
和foo.plg:
set terminal png
set outputname
f(x) = sin(x)
plot datafile
Run Code Online (Sandbox Code Playgroud)
如您所见,更多参数以半冒号传递(如在bash脚本中),但字符串变量需要用''(gnuplot语法,非Bash语法)封装
bp.*_*bp. 11
这个问题得到了很好的回答,但我认为我可以找到一个适合填补这里的利基,如果只是为了减少像我一样在网上搜索这个人的工作量.来自vagoberto的答案给了我解决我的问题版本所需要的东西,所以我将在这里分享我的解决方案.
我在最新的环境中开发了一个绘图脚本,这使我可以:
#!/usr/bin/gnuplot -c
set terminal png truecolor transparent crop
set output ARG1
set size 1, 0.2
rrLower = ARG2
rrUpper = ARG3
rrSD = ARG4
resultx = ARG5+0 # Type coercion required for data series
resulty = 0.02 # fixed
# etc.
Run Code Online (Sandbox Code Playgroud)
这在具有最近gnuplot(在我的情况下为5.0.3)的环境中从命令行执行得非常好.
$ ./plotStuff.gp 'output.png' 2.3 6.7 4.3 7
Run Code Online (Sandbox Code Playgroud)
当上传到我的服务器并执行时,它失败了,因为服务器版本是4.6.4(当前在Ubuntu 14.04 LTS上).下面的垫片解决了这个问题,而不需要对原始脚本进行任何更改.
#!/bin/bash
# GPlot v<4.6.6 doesn't support direct command line arguments.
#This script backfills the functionality transparently.
SCRIPT="plotStuff.gp"
ARG1=$1
ARG2=$2
ARG3=$3
ARG4=$4
ARG5=$5
ARG6=$6
gnuplot -e "ARG1='${ARG1}'; ARG2='${ARG2}'; ARG3='${ARG3}'; ARG4='${ARG4}'; ARG5='${ARG5}'; ARG6='${ARG6}'" $SCRIPT
Run Code Online (Sandbox Code Playgroud)
这两个脚本的组合允许参数从bash传递到gnuplot脚本,而不考虑gnuplot版本和基本上任何*nix.
如果您需要位置参数, @vagoberto 的答案似乎是最好的恕我直言,并且我有一个小的改进需要添加。
瓦戈贝托的建议:
#!/usr/local/bin/gnuplot --persist
THIRD=ARG3
print "script name : ", ARG0
print "first argument : ", ARG1
print "third argument : ", THIRD
print "number of arguments: ", ARGC
Run Code Online (Sandbox Code Playgroud)
被调用者:
$ gnuplot -c script.gp one two three four five
script name : script.gp
first argument : one
third argument : three
number of arguments: 5
Run Code Online (Sandbox Code Playgroud)
对于像我这样懒惰的打字者,可以使脚本可执行(chmod 755 script.gp
)
然后使用以下内容:
#!/usr/bin/env gnuplot -c
THIRD=ARG3
print "script name : ", ARG0
print "first argument : ", ARG1
print "third argument : ", THIRD
print "number of arguments: ", ARGC
Run Code Online (Sandbox Code Playgroud)
并将其执行为:
$ ./imb.plot a b c d
script name : ./imb.plot
first argument : a
third argument : c
number of arguments: 4
Run Code Online (Sandbox Code Playgroud)
小智 5
你甚至可以做一些shell魔术,例如:
#!/bin/bash
inputfile="${1}" #you could even do some getopt magic here...
################################################################################
## generate a gnuplotscript, strip off bash header
gnuplotscript=$(mktemp /tmp/gnuplot_cmd_$(basename "${0}").XXXXXX.gnuplot)
firstline=$(grep -m 1 -n "^#!/usr/bin/gnuplot" "${0}")
firstline=${firstline%%:*} #remove everything after the colon
sed -e "1,${firstline}d" < "${0}" > "${gnuplotscript}"
################################################################################
## run gnuplot
/usr/bin/gnuplot -e "inputfile=\"${inputfile}\"" "${gnuplotscript}"
status=$?
if [[ ${status} -ne 0 ]] ; then
echo "ERROR: gnuplot returned with exit status $?"
fi
################################################################################
## cleanup and exit
rm -f "${gnuplotscript}"
exit ${status}
#!/usr/bin/gnuplot
plot inputfile using 1:4 with linespoints
#... or whatever you want
Run Code Online (Sandbox Code Playgroud)
我的实现有点复杂(例如在sed调用中替换一些魔法标记,而我已经在它...),但我简化了这个例子以便更好地理解.你也可以让它更简单.... YMMV.
小智 5
在shell中写
gnuplot -persist -e "plot filename1.dat,filename2.dat"
Run Code Online (Sandbox Code Playgroud)
并连续获得您想要的文件。-persist 用于使 gnuplot 屏幕停留,只要用户不手动退出它。
归档时间: |
|
查看次数: |
117210 次 |
最近记录: |