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

Pou*_*uya 1 bash awk gnuplot

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

plot "<awk 'END{print $1"*cos(t)*cos("$2")-"$3"*sin(t)*sin("$4"), "$1"*cos(t)*sin("$2")+"$3"*sin(t)*cos("$4")"}' manip_file.csv"
Run Code Online (Sandbox Code Playgroud)

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

plotCMD = 'awk 'END{print "plot " $1"*cos(t)*cos("$2")-"$3"*sin(t)*sin("$4"), "$1"*cos(t)*sin("$2")+"$3"*sin(t)*cos("$4")"}' manip_file.csv'
eval(plotCMD)
Run Code Online (Sandbox Code Playgroud)

但这次 gnuplot 说';' expected. 如果我awk在命令行中运行命令,它会给我一个正确的方程,gnuplot 绘制它没有问题。因此,丢失一些单/双引号不是问题。试图逃避美元符号 ( \$1) 也没有解决问题。有什么想法吗?

Chr*_*oph 5

您将 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)
Run Code Online (Sandbox Code Playgroud)