问题
I was wondering if there is an easy way, using Gnuplot, to get the last point in a data file say data.txt and store the values in a variable.
From this question, accessing the nth datapoint in a datafile using gnuplot I know that I can get the X-Value using stats and GP_VAL_DATA_X_MAX variable, but is there a simple trick to get the corresponding y-value?
回答1:
If you want to use Gnuplot, you can
plot 'data.txt'
plot[GPVAL_DATA_X_MAX:] 'data.txt'
show variable GPVAL_DATA_Y_MAX
OR
plot 'data.txt'
plot[GPVAL_DATA_X_MAX:] 'data.txt'
print GPVAL_DATA_Y_MAX
回答2:
A third possibility is to write each ordinate value into the same user variable during plotting. Last value stays in:
plot dataf using 1:(lasty=$2)
print lasty
回答3:
If you know how your file is organised (separators, trailing empty lines) and you have access to standard Unix tools, you make use of Gnuplot’s system command. For example, if you have no trailing newlines and your values are separated by tabs, you can do the following:
x = system("tail -n 1 data.txt | cut -f 1")
y = system("tail -n 1 data.txt | cut -f 2")
(tail gets the last n lines of a file. cut extracts the column f.)
Note that x and y are strings if obtained this way, but for most applications this should not matter. If you must convert them, you can still add zero.
来源:https://stackoverflow.com/questions/37278786/gnuplot-store-last-data-point-as-variable