Reputation: 133
I am reading multiple netcdf files using the ncread
function in matlab.
For a reason unknown to me, some files (described by FILEPATH
in the following) are not properly read and ncread
crashes, producing the error message:
Error using internal.matlab.imagesci.nc/openToRead (line 1259)
Could not open FILEPATH for reading.
Error in internal.matlab.imagesci.nc (line 121) this.openToRead();
Error in ncread (line 53)
ncObj = internal.matlab.imagesci.nc(ncFile);
Error in MY_FUNCTION (line 102)
Lon = nanmean(ncread(FILEPATH,'Lon'));
If you know of a method to test netcdf files without crashing, or if you understand what produces this error, any insight would be appreciated.
Upvotes: 0
Views: 1003
Reputation:
The standard way is to wrap the possibly-failing statement in a try
/catch
statement to intercept the thrown exception before interrupting the function execution, like this:
function [out1, out2, ...] = MY_FUNCTION(arg1, arg2, ...)
%//Initial code
try
Lon_data = ncread(FILEPATH,'Lon');
catch ME
warning('MY_FUNCTION:ncread', 'Could not load because <<%s>>',ME.message);
%//Do something to recover from error
%//Return from function if recover not possible
end;
Lon = nanmean(Lon_data);
%//Rest of the code
end
Please note that ...
in the function signature above is not valid MATLAB syntax, but rather something that says "here are some inputs and outputs that I don't know how they are declared"; please substitute with your proper in/out declaration.
Upvotes: 1