1

I am trying to plot a parametric expression in a gnuplot script which its coefficient are stored in the last row of text file. To do so, firstly I tried this:

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"

but gnuplot says undefined variable: t. So next I tried the following:

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)

But this time gnuplot says ';' expected. If I run the awk command in the command line it gives me a correct equation which gnuplot has not problem plotting it. Hence it is not a problem of missing some single/double quotations. Trying to escape the dollar signs (\$1) didn't solve the problem as well. Any thoughts?

Pouya
  • 1,266
  • 3
  • 18
  • 44
  • You are probably looking for [How to use shell variables in awk script](http://stackoverflow.com/q/19075671/1983854) – fedorqui Nov 16 '15 at 13:28

1 Answers1

5

You are completely mixing gnuplot and awk syntax together. Here is a solution with 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)
Christoph
  • 47,569
  • 8
  • 87
  • 187
  • Thank you very much. Quick question: Why have you multiplied `word(p, i)` by 1.0? Is it sort of a hack for typecasting? – Pouya Nov 16 '15 at 16:35
  • Yes. Probably it would also work without this multiplication, since you multiply the parameters again inside the functions. But I wanted to point out this "typecast" – Christoph Nov 16 '15 at 18:56