MATLAB: Determine if variable name exists in netcdf file.

MATLABnetcdf

I need to check if a variable name exist in a netcdf file. Is there a way to do this without creating a list variable names and searching it entry by entry in a loop?
I think I can catch it with the warnings produced:
>> a=netcdf.inqVarID(ncid,'u_d')
Error using netcdflib
The NetCDF library encountered an error during execution of 'inqVarID' function - 'Variable not found (NC_ENOTVAR)'.
Error in netcdf.inqVarID (line 22)
varid = netcdflib('inqVarID', ncid, varname);
but Im not familiar with warnings.

Best Answer

Not sure if this is the best way but it works and its fast:
file = 'C:\Users\Jason\Documents\MATLAB\sgData\sg135\p1350050.nc';
ncid = netcdf.open(file,'nowrite');
str = 'good';
var = 'u_da';
try
ID = netcdf.inqVarID(ncid,var);
catch exception
if strcmp(exception.identifier,'MATLAB:imagesci:netcdf:libraryFailure')
str = 'bad';
end
end
netcdf.close(ncid)
disp(str)
Related Question