www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/ReadFromNetCDFFileUsingLowLevelFunctionsExample.m
%% Read from NetCDF File Using Low-Level Functions % This example shows how to get information about the dimensions, % variables, and attributes in a NetCDF file using MATLAB low-level % functions in the |netcdf| package. To use these functions effectively, % you should be familiar with the NetCDF C Interface. %% Open NetCDF File % Open the sample NetCDF file, |example.nc|, using the |netcdf.open| % function, with read-only access. % Copyright 2015 The MathWorks, Inc. ncid = netcdf.open('example.nc','NC_NOWRITE') %% % |netcdf.open| returns a file identifier. %% Get Information About NetCDF File % Get information about the contents of the file using the |netcdf.inq| % function. This function corresponds to the |nc_inq| function in the NetCDF library C API. [ndims,nvars,natts,unlimdimID] = netcdf.inq(ncid) %% % |netcdf.inq| returns the number of dimensions, variables, and global % attributes in the file, and returns the identifier of the unlimited % dimension in the file. An unlimited dimension can grow. %% % Get the name of the global attribute in the file using the % |netcdf.inqAttName| function. This function corresponds to the % |nc_inq_attname| function in the NetCDF library C API. To get the name of % an attribute, you must specify the ID of the variable the attribute is % associated with and the attribute number. To access a global attribute, % which is not associated with a particular variable, use the constant % |'NC_GLOBAL'| as the variable ID. global_att_name = netcdf.inqAttName(ncid,... netcdf.getConstant('NC_GLOBAL'),0) %% % Get information about the data type and length of the attribute using the % |netcdf.inqAtt| function. This function corresponds to the |nc_inq_att| % function in the NetCDF library C API. Again, specify the variable ID % using |netcdf.getConstant('NC_GLOBAL')|. [xtype,attlen] = netcdf.inqAtt(ncid,... netcdf.getConstant('NC_GLOBAL'),global_att_name) %% % Get the value of the attribute, using the |netcdf.getAtt| function. global_att_value = netcdf.getAtt(ncid,... netcdf.getConstant('NC_GLOBAL'),global_att_name) %% % Get information about the first dimension in the file, using the % |netcdf.inqDim| function. This function corresponds to the |nc_inq_dim| % function in the NetCDF library C API. The second input to |netcdf.inqDim| % is the dimension ID, which is a zero-based index that identifies the % dimension. The first dimension has the index value |0|. [dimname,dimlen] = netcdf.inqDim(ncid,0) %% % |netcdf.inqDim| returns the name and length of the dimension. %% % Get information about the first variable in the file using the % |netcdf.inqVar| function. This function corresponds to the |nc_inq_var| % function in the NetCDF library C API. The second input to |netcdf.inqVar| % is the variable ID, which is a zero-based index that identifies the % variable. The first variable has the index value |0|. [varname,vartype,dimids,natts] = netcdf.inqVar(ncid,0) %% % |netcdf.inqVar| returns the name, data type, dimension ID, and the number % of attributes associated with the variable. The data type information % returned in |vartype| is the numeric value of the NetCDF data type % constants, such as, |NC_INT| and |NC_BYTE|. See the NetCDF documentation for % information about these constants. %% Read Data from NetCDF File %% % Read the data associated with the variable, |avagadros_number|, in the % example file, using the |netcdf.getVar| function. The second input to % |netcdf.getVar| is the variable ID, which is a zero-based index that % identifies the variable. The |avagadros_number| variable has the index value |0|. A_number = netcdf.getVar(ncid,0) %% % View the data type of |A_number|. whos A_number %% % The functions in the |netcdf| package automatically choose the MATLAB % class that best matches the NetCDF data type, but you can also specify % the class of the return data by using an optional argument to % |netcdf.getVar|. %% % Read the data associated with |avagadros_number| and return the data as class |single|. A_number = netcdf.getVar(ncid,0,'single'); whos A_number %% Close NetCDF File % Close the NetCDF file, |example.nc|. netcdf.close(ncid)