如何将变量传递给 gnuplot 的 -e 参数?

3
我想定义一个 png 文件的 gnuplot 输出。如何在 gnuplot 中定义 fileName
    #This lines and also Traffic$j are define on my bash file.
    # Traffic$j is the name of file and that is valid. Traffic$j is on the loop
    # j is loop index
.
.

    fileName=Traffic$j
.
.

我尝试这样做:
gnuplot -e "filename=${!fileName}" plotFile

但我遇到了这个错误:

行 0:需要常量表达式

我尝试了ruakh的想法:
gnuplot -e "filename = '${!fileName}'" plotFile

但我收到了这个警告:

"plotFile",第12行:警告:跳过没有有效数据点的数据文件

第12行?看看我脚本的最后一行。

我如何将变量传递给 gnuplot 的 -e 开关?

更新: 我的 plotFile 如下:

set terminal png size 720,450 enhanced font "H,11"
set output filename . '.png'

plot '../_numXY' using 2:3:(sprintf('%d', $1)) with labels offset 0,1 point pointtype 7 ps 2 lc rgb "green" notitle, \
filename using 1:2:($3-$1):($4-$2) with vectors linewidth 6 lc rgb "blue" notitle #line 12

1
可能是重复的问题:如何将命令行参数传递给gnuplot? - Christoph
2个回答

1
问题不在于如何传递变量,而在于如何引用字符串。例如,如果$j3$Traffic3file.txt,那么传递给-e的是filename=file.txt,而你需要传递的是类似于filename = "file.txt"filename = 'file.txt'这样的内容。因此:
gnuplot -e "filename = '${!fileName}'" plotFile

编辑添加: 在评论中,您写道:

谢谢,$Traffic3不是file.txt。或者更好的说法是我没有$Traffic3,但我有Traffic3。Traffic3不是参数。它是文件本身的名称,而不是指向像file.txt这样的另一个东西。

这意味着您不应该编写${!fileName},而应该编写$fileName。符号${!fileName}的意思是大致上,“好的,那么$fileName的值是另一个变量的名称。给我那个变量的值。”所以你只需要:

gnuplot -e "filename = '$fileName'" plotFile

谢谢,$Traffic3不是file.txt。或者更好地说,我没有$Traffic3,但我有Traffic3。Traffic3不是参数。它是文件本身的名称,而不是指像file.txt这样的其他东西。 - alex
1
@alex:在这种情况下,你不应该写 ${!fileName}(它的意思是“变量名为 $fileName 值的变量的值 -- 例如,如果 $fileNameTraffic3,那么 ${!fileName} 的意思就是 $Traffic3),而应该写 $fileName。我会更新我的回答。 - ruakh

0

你可能不想进行间接变量扩展转换?

gnuplot -e "filename=${fileName}" plotFile

感谢您的帮助。但它告诉我:第0行:Traffic1未定义变量。 - alex
1
因为应该是 gnuplot -e "filename='${fileName}'" plotFile。请参见如何将命令行参数传递给gnuplot? - Christoph
@Christoph,谢谢你,Christoph。上帝保佑你,你让我的一天变得美好。谢谢。 - alex

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接