mtk
mtk

Reputation: 13709

gnuplot how to give column number to plot dynamically?

I am automating a task to create small graphs using gnuplot. And I need to pass the column number from the datafile that is to be plotted

pfile=system("echo $file")
colnum=system("echo $colnum")

plot pfile using 4:(column(colnum)) title "slot1"
                     ^^^^^^^^^^^^

colnum is being exported earlier as export colnum=2

I am getting error at the highlighted part. I tried using export/fetch by system command, but it didn't work either e.g. I tried 4:colnum, got similar error

"./12.gnuplot.helper.pg", line 29: warning: Skipping data file with no valid points    
plot pfile using 4:(column(colnum)) title "slot1"   
                                                ^   
"./12.gnuplot.helper.pg", line 29: x range is invalid

Upvotes: 2

Views: 5134

Answers (3)

vagoberto
vagoberto

Reputation: 2442

This is an alternative to @EWCZ's answer. Any variable initialized using the system command is treated as a string: in your case, colnum="2" (with quotes). You can transform it to an integer by adding zero:

# first option
plot pfile using 4:(column(colnum+0))

# second option
colnum = system("echo $colnum") + 0
plot pfile using 4:(column(colnum))

As a side note, your task could be more flexible (easier to automate) by using @Christoph's answer; better if you have gnuplot 5.0 because you can pass arguments to a gnuplot script directly

Upvotes: 2

ewcz
ewcz

Reputation: 13087

it seems that one has to do colnum=int(system("echo $colnum")) so that the variable colnum is interpreted as an integer and not a string

Upvotes: 1

Christoph
Christoph

Reputation: 48390

You could also pass the column number via command line

gnuplot -e "colnum=$colnum" script.gp

and your script.gp contains

plot pfile using 4:(column(colnum))

Upvotes: 4

Related Questions