将超过 10 个参数传递给 gnuplot 脚本

Passing more then 10 arguments to gnuplot script

在 gnuplot 5.2 手册中很好地解释了可以使用字符串变量 ARG0、ARG1、...、ARG9 和整数变量 ARGC 将变量传递给脚本。 例如,如果我执行

gnuplot -persist -c "script1.gp" "sin(x)" 1.23 "This is a plot title"

ARG0 holds "script1.gp"
ARG1 holds the string "sin(x)"
ARG2 holds the string "1.23"
ARG3 holds the string "This is a plot title"
ARGC is 3

有没有办法将超过 10 个参数传递给 gnuplot 脚本?

使用两个文件你可以使用类似这样的东西。

在此示例中,第一个脚本 (send.plt) 将 "arguments" 作为单个字符串发送到第二个脚本。

Arg2Sent = "This is a long string send to another script as a single argument"
call "receive.plt" sprintf("%s", Arg2Sent)

第二个脚本(receive.plt)包含:

array Arg[words(ARG1)]

do for [i=1:|Arg|]{
    Arg[i] = word(ARG1, i)
    print sprintf("Arg[%2g]: %s", i, Arg[i])
}

输出

Arg[ 1]: This
Arg[ 2]: is
Arg[ 3]: a
Arg[ 4]: long
Arg[ 5]: string
Arg[ 6]: send
Arg[ 7]: to
Arg[ 8]: another
Arg[ 9]: script
Arg[10]: as
Arg[11]: a
Arg[12]: single
Arg[13]: argument

另一个例子,这次使用数字。

Arg2Sent = "0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765"
call "receive.plt" sprintf("%s", Arg2Sent)

输出

Arg[ 2]: 1
Arg[ 3]: 1
Arg[ 4]: 2
Arg[ 5]: 3
Arg[ 6]: 5
Arg[ 7]: 8
Arg[ 8]: 13
Arg[ 9]: 21
Arg[10]: 34
Arg[11]: 55
Arg[12]: 89
Arg[13]: 144
Arg[14]: 233
Arg[15]: 377
Arg[16]: 610
Arg[17]: 987
Arg[18]: 1597
Arg[19]: 2584
Arg[20]: 4181
Arg[21]: 6765

一个很长的例子

Arg2Sent = "'first filename with spaces.txt' \
'second filename with spaces.txt' \
'third filename with spaces.txt' \
'fourth filename with spaces.txt' \
'fifth filename with spaces.txt' \
'sixth filename with spaces.txt' \
'seventh filename with spaces.txt' \
'eighth filename with spaces.txt' \
'ninth filename with spaces.txt' \
'tenth  filename with spaces.txt' \
'eleventh filename with spaces.txt' \
'twelfth filename with spaces.txt' \
'thirteenth filename with spaces.txt' \
'fourteenth filename with spaces.txt' \
'fifteenth filename with spaces.txt' \
'sixteenth filename with spaces.txt' \
'seventeenth filename with spaces.txt' \
'eighteenth filename with spaces.txt' \
'nineteenth filename with spaces.txt' \
'twentieth filename with spaces.txt'"

call "receive.plt" sprintf("%s", Arg2Sent)

输出

Arg[ 1]: first filename with spaces.txt
Arg[ 2]: second filename with spaces.txt
Arg[ 3]: third filename with spaces.txt
Arg[ 4]: fourth filename with spaces.txt
Arg[ 5]: fifth filename with spaces.txt
Arg[ 6]: sixth filename with spaces.txt
Arg[ 7]: seventh filename with spaces.txt
Arg[ 8]: eighth filename with spaces.txt
Arg[ 9]: ninth filename with spaces.txt
Arg[10]: tenth  filename with spaces.txt
Arg[11]: eleventh filename with spaces.txt
Arg[12]: twelfth filename with spaces.txt
Arg[13]: thirteenth filename with spaces.txt
Arg[14]: fourteenth filename with spaces.txt
Arg[15]: fifteenth filename with spaces.txt
Arg[16]: sixteenth filename with spaces.txt
Arg[17]: seventeenth filename with spaces.txt
Arg[18]: eighteenth filename with spaces.txt
Arg[19]: nineteenth filename with spaces.txt
Arg[20]: twentieth filename with spaces.txt

最后一个例子

Arg2Sent = "data.dat 1 2 'lp pt 6' 'pi -1' blue 'A randon example'"

call "receive.plt" sprintf("%s", Arg2Sent)

receive.plt包含

array Arg[words(ARG1)]

do for [i=1:|Arg|]{
    Arg[i] = word(ARG1, i)
}

filename     = sprintf("%s", Arg[1])
firstcolumn  = int(Arg[2])
secondcolumn = int(Arg[3])
plottype     = sprintf("%s", Arg[4])
pointstyle   = sprintf("%s", Arg[5])
color        = sprintf("'%s'", Arg[6])
title        = sprintf("%s", Arg[7])

plot filename u firstcolumn:secondcolumn w @plottype @pointstyle lc @color t title 

输出