Gnuplot: store last data point as variable

二次信任 提交于 2019-12-22 06:50:43

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!