【问题标题】:Using eval with awk expression in gnuplot script在 gnuplot 脚本中使用 eval 和 awk 表达式
【发布时间】:2015-11-16 13:26:38
【问题描述】:

我正在尝试在 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) 也没有解决问题。有什么想法吗?

【问题讨论】:

标签: bash awk gnuplot


【解决方案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)

【讨论】:

  • 非常感谢。快速提问:为什么你将word(p, i) 乘以 1.0?这是一种用于类型转换的技巧吗?
  • 是的。可能没有这种乘法它也可以工作,因为您在函数内部再次乘以参数。但我想指出这种“类型转换”
猜你喜欢
  • 2016-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多