Read data from a NetCDF file using the high-level functions, and
then read the file by using the
netcdf package low-level
Network Common Data Form (NetCDF) is a set of software libraries and machine-independent data formats that support the creation, access, and sharing of array-oriented scientific data. NetCDF is used by a wide range of engineering and scientific fields that want a standard way to store data so that it can be shared.
MATLAB® high-level functions simplify the process of importing data from a NetCDF file or an OPeNDAP NetCDF data source. MATLAB low-level functions enable more control over the importing process, by providing access to the routines in the NetCDF C library. To use the low-level functions effectively, you should be familiar with the NetCDF C Interface. The NetCDF documentation is available at the Unidata website.
For information about importing Common Data Format (CDF) files, which have a separate, incompatible format, see Import CDF Files Using Low-Level Functions.
This example shows how to display and read the contents of a NetCDF file, using high-level functions.
Display the contents of the sample NetCDF file,
Source: \\matlabroot\toolbox\matlab\demos\example.nc Format: netcdf4 Global Attributes: creation_date = '29-Mar-2010' Dimensions: x = 50 y = 50 z = 5 Variables: avagadros_number Size: 1x1 Dimensions: Datatype: double Attributes: description = 'this variable has no dimensions' temperature Size: 50x1 Dimensions: x Datatype: int16 Attributes: scale_factor = 1.8 add_offset = 32 units = 'degrees_fahrenheight' peaks Size: 50x50 Dimensions: x,y Datatype: int16 Attributes: description = 'z = peaks(50);' Groups: /grid1/ Attributes: description = 'This is a group attribute.' Dimensions: x = 360 y = 180 time = 0 (UNLIMITED) Variables: temp Size:  Dimensions: x,y,time Datatype: int16 /grid2/ Attributes: description = 'This is another group attribute.' Dimensions: x = 360 y = 180 time = 0 (UNLIMITED) Variables: temp Size:  Dimensions: x,y,time Datatype: int16
ncdisp displays all the groups, dimensions, and
variable definitions in the file. Unlimited dimensions are identified with
Read data from the
peaksData = ncread('example.nc','peaks');
Display information about the
Name Size Bytes Class Attributes peaksData 50x50 5000 int16
description attribute associated with the
peaksDesc = ncreadatt('example.nc','peaks','description')
peaksDesc = z = peaks(50);
Create a three-dimensional surface plot of the variable data. Use the
value of the
description attribute as the title of the
description attribute associated with the
/grid1/ group. Specify the group name as the second
input to the
g = ncreadatt('example.nc','/grid1/','description')
g = This is a group attribute.
Read the global attribute,
creation_date. For global
attributes, specify the second input argument to
creation_date = ncreadatt('example.nc','/','creation_date')
creation_date = 29-Mar-2010
This example shows how to find all unlimited dimensions in a group in a NetCDF file, using high-level functions.
Get information about the
/grid2/ group in the sample
example.nc, using the
ginfo = ncinfo('example.nc','/grid2/')
ginfo = Filename: '\\matlabroot\toolbox\matlab\demos\example.nc' Name: 'grid2' Dimensions: [1x3 struct] Variables: [1x1 struct] Attributes: [1x1 struct] Groups:  Format: 'netcdf4'
ncinfo returns a structure array containing
information about the group.
Get a vector of the Boolean values that indicate the unlimited dimensions for this group.
unlimDims = [ginfo.Dimensions.Unlimited]
unlimDims = 0 0 1
unlimDims vector to display the unlimited
Name: 'time' Length: 0 Unlimited: 1
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.
ncid = netcdf.open('example.nc','NC_NOWRITE')
ncid = 65536
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)
ndims = 3
nvars = 3
natts = 1
unlimdimID = -1
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)
global_att_name = 'creation_date'
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
[xtype,attlen] = netcdf.inqAtt(ncid,... netcdf.getConstant('NC_GLOBAL'),global_att_name)
xtype = 2
attlen = 11
Get the value of the attribute, using the
global_att_value = netcdf.getAtt(ncid,... netcdf.getConstant('NC_GLOBAL'),global_att_name)
global_att_value = '29-Mar-2010'
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
[dimname,dimlen] = netcdf.inqDim(ncid,0)
dimname = 'x'
dimlen = 50
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
[varname,vartype,dimids,natts] = netcdf.inqVar(ncid,0)
varname = 'avagadros_number'
vartype = 6
dimids = 
natts = 1
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_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
A_number = netcdf.getVar(ncid,0)
A_number = 6.0221e+23
View the data type of
Name Size Bytes Class Attributes A_number 1x1 8 double
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
Read the data associated with
avagadros_number and return the data as class
A_number = netcdf.getVar(ncid,0,'single'); whos A_number
Name Size Bytes Class Attributes A_number 1x1 4 single
Close NetCDF File
Close the NetCDF file,