2

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
mtk
  • 13,221
  • 16
  • 72
  • 112

3 Answers3

4

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))
Christoph
  • 47,569
  • 8
  • 87
  • 187
  • Thanks for the answer. Tried this, but it gives a similar error. – mtk Sep 27 '15 at 19:44
  • I tested it and it works fine. Note, that you must *not* have quotes around `$colnum`. Its all about integer vs. string. – Christoph Sep 28 '15 at 06:45
2

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

Community
  • 1
  • 1
vagoberto
  • 2,372
  • 20
  • 30
1

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

ewcz
  • 12,819
  • 1
  • 25
  • 47