lourencoj
lourencoj

Reputation: 362

Gnuplot, how to *skip* missing data files?

Depending on various factors i may not have 1 or more data files, referenced in pre-defined gnuplot plot instructions, that don't exist. When this is the case i get "warning: Skipping unreadable file" which cancels the rest of the instructions.

Is there any way i can ask gnuplot to skip any missing data files and plot all of the existing ones?

Upvotes: 10

Views: 5373

Answers (2)

boclodoa
boclodoa

Reputation: 698

Here is a similar solution without a helper script

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

the + 0 part is to convert the result from string to integer, in this way you can also use the negation

if ( ! file_exists("mydatafile") ) print "mydatafile not found."

Upvotes: 12

mgilson
mgilson

Reputation: 309831

Unfortunately, I can't seem to figure out how to do this without a simple helper script. Here's my solution with the "helper":

#!/bin/bash
#script ismissing.sh.  prints 1 if the file is missing, 0 if it exists.
test -e $1
echo $?

Now, make it executable:

chmod +x ismissing.sh

Now in your gnuplot script, you can create a simple function:

is_missing(x)=system("/path/to/ismissing.sh ".x)

and then you guard your plot commands as follows:

if (! is_missing("mydatafile") ) plot "mydatafile" u 1:2 ...

EDIT

It appears that gnuplot isn't choking because your file is missing -- The actual problem arises when gnuplot tries to set the range for the plot from the missing data (I assume you're autoscaling the axis ranges). Another solution is to explicitly set the axis ranges:

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

Upvotes: 2

Related Questions