【问题标题】:gnuplot : how to save datafile values into a variable (with condition)?gnuplot:如何将数据文件值保存到变量中(有条件)?
【发布时间】:2014-02-14 01:22:10
【问题描述】:

我使用 Gnuplot,我想将数据文件的值保存到带有条件的变量中。 例如,我有以下数据文件'example.dat':

columnl   column2
   5        7.0
   3        4.0
   7        6.0

在我的 gnuplot 脚本中,我希望能够编写:

variable = " in the file 'example.dat' ($1)==5 ? $2 : 1/0 "

在此处与以下内容相同:

variable = 7.0

当然值取决于数据文件。

那么有可能吗?

如果不是,是否可以无条件?

【问题讨论】:

  • 这里的一个答案可能有帮助:stackoverflow.com/questions/7540614/…
  • 您可以使用stats 'example.dat' using ($1 == 5 ? $2 : 0); variable = STATS_sum,但这要求您要提取的值是唯一定义的,即只有一行与条件匹配。

标签: file variables save gnuplot


【解决方案1】:

这就是我解决问题的方法:

A=""
plot file u (A=A.sprintf(' %g',$2), $1):2

第 2 列中的所有条目都将写入 A,然后您可以通过以下方式访问:

word(A,i)

其中 i 是原始数(从 1 开始) words(A) 给你总长度。

【讨论】:

    【解决方案2】:

    只是为了记录,您可以解决 Christoph 解决方案的限制,即您的数据中只允许 一次 出现您的支票值。

    返回对应值的两个版本:

    1. 对于您的检查值第一次出现
    2. 检查值最后一次出现

    以下示例适用于 gnuplot >=5.0.0,但如果您使用文件而不是数据块 $Data 它也适用于 gnuplot 4.6.0(OP 提出问题时的版本)。

    代码:

    ### 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-29
      • 2013-11-10
      • 1970-01-01
      • 2019-08-25
      • 1970-01-01
      • 1970-01-01
      • 2016-10-25
      相关资源
      最近更新 更多