7

Imagine I have a data file with two column. In gnuplot running

stats 'datafile' u 1:2

allows me find the minimum and maximum for the two columns. The variable STATS_index_min_x and STATS_index_min_y give respectively the index of the minimum for the first and second column. It says in the documentation that they are such that

data[STATS_index_min_x] == STATS_min_x

Now imagine that need to have access to data[STATS_index_min_x-1] or data[STATS_index_min_x+1]. How can I do that ? Actually how can I access any particular data of any column in gnuplot?

4 Answers 4

7

As far as I know, you cannot access the data in your file in this way (i.e. like an array).

On Linux/cygwin, you could work with an helper function that extracts the value directly from the underlying datafile using awk. You can define the following function:

getValue(row,col,filename) = system('awk ''{if (NR == '.row.') print $'.col.'}'' '.filename.'')

If your datafile is called datafile and contains these values:

1  1.4
2  4.3
3  2.5
4  0.0
5  9.0

Which gives:

gnuplot> print getValue(3,1,"datafile")
3
gnuplot> print getValue(1,2,"datafile")
1.4
2
  • 1
    How the code above would differ if one needs to store the value in a variable for later use? i.e. c = getValue(3,1,"datafile") instead of print getValue(3,1,"datafile")? Thank you
    – Bertrand
    Commented Mar 17, 2020 at 18:35
  • Apparently adding real() in front of system helps!
    – Bertrand
    Commented Mar 17, 2020 at 18:48
3

Gnuplot hadn't been designed for this, but you can cheat a little bit ;-)

stats 'datafile' u 1:2
plot[STATS_index_max_x+1:STATS_index_max_x+1.5] 'datafile.dat' u 0:1

then GPVAL_DATA_Y_MIN (and GPVAL_DATA_Y_MAX) contains the value you wanted.


NOTE

plot '<datafile>' u 0:<column>

plots the desidered column versus 0,1,2...

3

You could use stats combined with every to select the data. The following should print the values you are looking for:

stats 'datafile' u 1
imin = STATS_index_min
stats 'datafile' u 1 every ::(imin-1)::(imin-1)
print STATS_min
stats 'datafile' u 1 every ::(imin+1)::(imin+1)
print STATS_min
0

Find out the line number for which column 5 in the input datafile has its maximum value:

stats 'thermal_site_1.dat' u 5

Load into the variable Tc the corresponding value of column 1 in the same datafile, via a 'fake' plotting command and an if conditional:

plot 'thermal_site_1.dat' u ($0==STATS_index_max ? Tc=$1 : NaN):5

You can see the desired value via:

print Tc

Not the answer you're looking for? Browse other questions tagged or ask your own question.