gnuplot:如何将数据文件值保存到变量中(有条件)? [英] gnuplot : how to save datafile values into a variable (with condition)?
问题描述
我使用 Gnuplot,我想将数据文件的值保存到带有条件的变量中.例如,我有以下数据文件'example.dat':
I use Gnuplot and I would like to save values of a datafile into a variable with condition. For example, I have the following datafile 'example.dat':
columnl column2
5 7.0
3 4.0
7 6.0
在我的 gnuplot 脚本中,我希望能够编写:
In my gnuplot script, I would like to be able to write:
variable = " in the file 'example.dat' ($1)==5 ? $2 : 1/0 "
在此处与以下内容相同:
which would be here the same as:
variable = 7.0
当然值取决于数据文件.
of course the value depends on the datafile.
那么有可能吗?
如果不是,是否可以无条件?
If not, is it possible without condition?
推荐答案
只是为了记录,您可以解决 Christoph 解决方案的限制,该解决方案只允许 一次 出现您的数据中的检查值.
Just for the records, you can workaround the limitation of Christoph's solution which allows only one occurrence of your check value in your data.
返回对应值的两个版本:
Two versions which return the corresponding value:
- 对于您的支票值第一次出现
- 对于您的检查值最后一次出现
以下示例适用于 gnuplot >=5.0.0,但如果您使用文件而不是数据块 $Data
它也适用于 gnuplot 4.6.0(当时的版本)OP的问题).
The example below works for gnuplot >=5.0.0, but if you use a file instead of the datablock $Data
it will also work for gnuplot 4.6.0 (version at the time of OP's question).
代码:
### extract values via condition
reset
$Data <<EOD
5 7.0
3 4.0
7 6.0
5 1.0
4 2.0
5 3.0
2 1.0
EOD
check = 5 # value to check
v=NaN
stats $Data u ($1==check ? v==v ? NaN : v=$2 : 0) nooutput
print sprintf("Value for first occurrence of %g: %g",check,v)
v=NaN
stats $Data u ($1==check ? v=$2 : 0) nooutput
print sprintf("Value for last occurrence of %g: %g",check,v)
### end of code
结果:
Value for first occurrence of 5: 7
Value for last occurrence of 5: 3
这篇关于gnuplot:如何将数据文件值保存到变量中(有条件)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!