根据从文件中读取的数据设置gnuplot标题



我有一个gnuplot脚本,它从每10秒添加一次的文件中读取数据。数据由两个用空格分隔的整数组成;脚本简单地将它们解释为x和y坐标,并绘制它们。我在一个无限循环中这样写:

while (1) {
plot "myfile" using...
pause 15
}

我想要的是能够在每次在文件顶部重新读取x坐标时在标题中设置一个新的时间戳-在伪代码中,如下所示:

while (1) {
if (x == 128)
set title "Timestamp: ".strftime("%a %b %d %T %Y UTC", time(0))
plot "myfile" using...
pause 15
}

gnuplot是否对此提供支持?

在进一步思考这个问题时,我试着这样做:

while (1) {
xc=`cat myfile | tail -1 | sed -e "s/  */ /g" | cut -d  -f1`
if (xc > 128) {
set title "New title"
}
plot "myfile" using...
pause 15
}

这样做的问题是xc只被正确地赋值一次——cat myfile…命令似乎只执行一次,不幸的是:尽管"myfile"不断向其添加数据,赋给xc的值始终是相同的值-无论从"myfile"中检索到什么值。第一次。

进一步挖掘后,似乎如果不是

xc=`cat myfile | tail -1 | sed -e "s/  */ /g" | cut -d  -f1`

xc=system("cat myfile | tail -1 | sed -e "s/  */ /g" | cut -d\  -f1")

xc变量在每次循环中都被正确更新。

获取文件最近被修改的时间非常依赖于系统,很可能是不可能的。如果使用当前时间就足够了,那么在任何unix/linux类系统上最简单的方法是

set title system("date")

system函数返回一个字符串,因此您可以通过将其嵌入到更长的字符串中来进一步修改它:

set title "This plot generated on " . system("date")

从你的描述中我明白了以下几点:

  • 在循环中每15秒绘制一个文件
  • 如果最后一个x值(即在文件末尾的第一列)大于128,则将时间戳作为图形标题

假设x值从<=128开始,这意味着:

  • 没有初始标题
  • 如果最后一个x值再次变为<=128,则标题将不会更新,除非最后一个x再次变为>128。

在下面的示例中,您可以通过按" "来停止无限循环。检查help bind

脚本:

### plot file regularly, updating title on condition
reset session
FILE = "SO72454727.dat"
Stop = 0
bind s "Stop=1"
while (!Stop) {
stats FILE u (x=$1) nooutput
if (x>128) {
set title "Timestamp: ".strftime("%a %b %d %T %Y UTC", time(0))
}
plot FILE u 1:2 w lp pt 7 lc "red"
pause 15
}
### end of script

最新更新