This is machine translation

Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Translate This Page

MathWorks Machine Translation

The automated translation of this page is provided by a general purpose third party translator tool.

MathWorks does not warrant, and disclaims all liability for, the accuracy, suitability, or fitness for purpose of the translation.

Import NetCDF Files and OPeNDAP Data

MATLAB NetCDF Capabilities

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.

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.

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.

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').

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)

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 0.

[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_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)

A_number = 6.0221e+23

View the data type of A_number.

whos A_number

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 netcdf.getVar.

Read the data associated with avagadros_number and return the data as class single.