Determine if variable name exists in netcdf file.

조회 수: 14 (최근 30일)
Jason
Jason 2012년 7월 27일
댓글: Vijay Sagar 2020년 7월 23일
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.

채택된 답변

Jason
Jason 2012년 7월 27일
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)

추가 답변 (1개)

francisco
francisco 2013년 5월 14일
Hello Jason
I have the same problem, can you help me on this please.
  댓글 수: 1
Vijay Sagar
Vijay Sagar 2020년 7월 23일
The above solution by @Jason worked for me. I had a different 'exception' for my files. I have repaced with that after that it worked successfuly for me.

댓글을 달려면 로그인하십시오.

태그

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by