在gnuplot脚本

时间:2015-11-16 13:26:38

标签: 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"

但是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)

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

1 个答案:

答案 0 :(得分:4)

您将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)