在 gnuplot 脚本中使用带有 awk 表达式的 eval

Using eval with awk expression in gnuplot script

我正在尝试在 gnuplot 脚本中绘制参数表达式,其系数存储在文本文件的最后一行中。为此,我首先尝试了这个:

plot "<awk 'END{print "*cos(t)*cos("")-""*sin(t)*sin(""), ""*cos(t)*sin("")+""*sin(t)*cos("")"}' manip_file.csv"

但是 gnuplot 说 undefined variable: t。所以接下来我尝试了以下方法:

plotCMD = 'awk 'END{print "plot " "*cos(t)*cos("")-""*sin(t)*sin(""), ""*cos(t)*sin("")+""*sin(t)*cos("")"}' manip_file.csv'
eval(plotCMD)

但是这次 gnuplot 说 ';' expected。如果我在命令行中 运行 awk 命令,它会给我一个正确的等式,gnuplot 在绘制它时没有问题。因此,缺少一些 single/double 引号不是问题。试图转义美元符号 ($1) 也没有解决问题。有什么想法吗?

您完全混合了 gnuplot 和 awk 语法。这是 gnuplot 的解决方案:

# 1. Get the last row of your text file
p = system('tail -1 manip_file.csv')

# 2. Get the four parameters, assuming they are white-space separated
p1 = word(p, 1)*1.0
p2 = word(p, 2)*1.0
p3 = word(p, 3)*1.0
p4 = word(p, 4)*1.0

# 3. Define the functions
x(t) = p1*cos(t)*cos(p2) - p3 * sin(t)*sin(p4)
y(t) = p1*cos(t)*sin(p2)+p3*sin(t)*cos(p4)

# 4. Plot them
set parametric
plot x(t), y(t)