Gnuplot,如何*跳过*丢失的数据文件?

lou*_*coj 9 gnuplot

根据各种因素,我可能没有一个或多个数据文件,在预定义的gnuplot绘图指令中引用,不存在.在这种情况下,我得到"警告:跳过不可读的文件",取消其余的说明.

有什么方法可以让gnuplot跳过任何丢失的数据文件并绘制所有现有的文件?

boc*_*doa 5

这是一个没有帮助脚本的类似解决方案

file_exists(file) = system("[ -f '".file."' ] && echo '1' || echo '0'") + 0
if ( file_exists("mydatafile") ) plot "mydatafile" u 1:2 ...
Run Code Online (Sandbox Code Playgroud)

+ 0部分是将结果从字符串转换为整数,这样你也可以使用否定

if ( ! file_exists("mydatafile") ) print "mydatafile not found."
Run Code Online (Sandbox Code Playgroud)


mgi*_*son 2

不幸的是,如果没有简单的帮助脚本,我似乎无法弄清楚如何做到这一点。这是我使用“助手”的解决方案:

#!/bin/bash
#script ismissing.sh.  prints 1 if the file is missing, 0 if it exists.
test -e $1
echo $?
Run Code Online (Sandbox Code Playgroud)

现在,使其可执行:

chmod +x ismissing.sh
Run Code Online (Sandbox Code Playgroud)

现在,在 gnuplot 脚本中,您可以创建一个简单的函数:

is_missing(x)=system("/path/to/ismissing.sh ".x)
Run Code Online (Sandbox Code Playgroud)

然后你按如下方式保护你的绘图命令:

if (! is_missing("mydatafile") ) plot "mydatafile" u 1:2 ...
Run Code Online (Sandbox Code Playgroud)

编辑

看来 gnuplot 并没有因为文件丢失而令人窒息——当 gnuplot 尝试根据丢失的数据设置绘图范围时(我假设您正在自动缩放轴范围),实际问题就会出现。另一个解决方案是显式设置轴范围:

set xrange [-10:10]
set yrange [-1:1]
plot "does_not_exist" u 1:2
plot sin(x)  #still plots
Run Code Online (Sandbox Code Playgroud)