test if netcdf file is valid without crashing - matlab

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.

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.

Related

Error of non-existent field in matlab

I am new to MATLAB and have some code that I am running.I get an error that says the following in the command window:
error of non-existent field 'W'
Error in schedule2control (line 7)
ui = vertcat(schedule.control(c).W(:).val);
I understand that its telling me the variable W is non-existent.
If that is the case should that variable not show up under work space??
The variable its telling me is non existent (W) exists in my work space.
Any help/clarification would be great.

matlab function not working surprisingly

I have created this function :
function Calcul_Constantes ( xBin1 , xTin1 , xHin1 )
% computes global variables that change depending on the circumstances
global v rhoc K1v K2v xBin xTin xHin cBin cTin cHin;
xBin=xBin1;
xTin=xTin1;
xHin=xHin1;
rhoc = 0.02777*(2.106*xHin+78.12*xBin)*(6.935*xHin+23.15*xBin);
K1v=0.6*rhoc/175;
K2v=(2.70803*10^-4+7.5*10^-4*v*rhoc)/175;
cBin=0.02777*xBin;
cTin=0.02777*xTin;
cHin=0.02777*xHin;
end
and when I do test in my main script :
Calcul_Constantes(0,0,1);
xBin
xHin
the following error occures:
Error using Calcul_Constantes
Too many input arguments.
Error in Mercredi15_main (line 48)
Calcul_Constantes(0,0,1);
I'd be grateful for any help, I really can't see what does not work
Probably there is another Calcul_Constantes function somewhere else. You might have saved another version of Calcul_Constantes function somewhere.
In command line type :
which Calcul_Constantes
and check if the return directory and .m file is the which you are trying to use. Rename or delete the wrong function.

Matlab 'main' function isn't reading local functions

I have a code in matlab (~1000 lines) that constists of about 15 functions. The code runs fine with each function as a different script, but I want to put them all into one file so I can use the publish function more easily. However, when I put them all together my 'main' function isn't recognizing the local functions. Here's what it looks like:
function full_function()
...
values = fvalues(0);
...
end
function values = fvalues(state)
...
end
When I try to run it, it gives me
"Undefined function 'fvalues' for input arguments
of type 'double'.
Error in full_function (line 32)
values = fvalues(0);"
I've looked all over for how to do local functions and I have no idea what I'm doing wrong. If I right-click on fvalues and hit 'open' it even brings me to the correct portion of the code, so I have no idea why full_function cannot read it. Please help! Thanks.

Matlab: How to catch warning

I am running some data processing work in MATLAB and solver uses BACKSLASH operator. Sometimes, I get warning like this:
Warning: Rank deficient, rank = 1390, tol = 1.335195e-010.
Warning: Rank deficient, rank = 1386, tol = 1.333217e-010.
I would like to catch those warnings.
I am trying to convert warning to error and then catch it as described here under title “Trapping warnings”:
http://undocumentedmatlab.com/blog/trapping-warnings-efficiently
In the example, following string has been used to convert warning to error:
s = warning('error', 'MATLAB:DELETE:Permission');
However, I am not sure what string to use for my case. I tried using
s = warning('error', 'Warning: Rank deficient’);
But, it did not work.
Any help would be appreciated.
Regards,
DK
You need to specify the warning identifier, not the warning text. You can find the identifier using the two-output form of lastwarn:
[msgstr, msgid] = lastwarn
In your case, I think the identifier you want is 'MATLAB:rankDeficientMatrix'.
You could try to use lastwarn as an alternative. After your division, call it and compare it with strcmp to the usual warning message, and if its the one you wnat you could manually throw the error you want with error.
As you suggested: you can reset lastwarn throwing an empty warning warning('')

Error with struct2table command

I am getting the following error while running a program.
??? Undefined function or method 'struct2table' for input arguments of type 'struct'.
Error in ==> cellarray at 13
T=struct2table(parameter,'AsArray',true);
The program is as under
a=10;
b=15;
parameter(a).alpha_star=0;
parameter(b).gamma_star=0;
x=5;
for j=1:b
for i=1:a
parameter(i).alpha_star=x+i;
end
parameter(j).gamma_star = x^2+j;
end
T=struct2table(parameter,'AsArray',true);
Can you please tell me, where am I wrong?
I am guessing you have a matlab older than R2013b, and since do not have this function (you can check that via which struct2table).
you can try to work around it by using struct2array and reshape.
you can also use
alp=[];
gam =[];
for i=1:length(parameter)
if ~isempty(parameter(i).alpha_star)
alp(end+1)=(parameter(i).alpha_star);
end
if ~isempty(parameter(i).gamma_star)
gam(end+1)=(parameter(i).gamma_star);
end
end
to extract the values to seperate arrays and
fieldnames(parameter);
to get the fieldnames, this is dirty but if it is only needed for presentation...