Convert string to integer in gnuplot

Viewed 1122

How gnuplot convert a string to integer

I wonder if there is an easy way to convert a string to integer. For example, I want a way as like strtointeger("2") to return an integer 2.

columns="2 3"

plot for [i in columns] "mydata.dat" using 1:strtointeger(i)

1 Answers

The variable i in [i in columns] is actually a string. So, convert it to an integer via int(i).

If you want to plot a column defined by an integer variable, use ... using (column(i))....

So, in combination:

Code:

### plot column numbers from a sequence in a string
reset session
set key top left

$Data <<EOD
11 12 13 14 15
21 22 23 24 25
31 32 33 34 35
41 42 43 44 45
51 52 53 54 55
EOD

columns="2 3 5 1 4"

plot for [i in columns] $Data using 1:(column(int(i))) w lp title sprintf("Column %s",i)
### end of code

In your case remove the datablock $Data <<EOD ... EOD and in the plot command replace $Data with "mydata.dat"

Result:

enter image description here

Related