2

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?

1

1 Answer 1

6

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much. Quick question: Why have you multiplied word(p, i) by 1.0? Is it sort of a hack for typecasting?
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"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.