Main Content

Map NetCDF API Syntax to MATLAB Syntax

MATLAB® provides access to the functions in the netCDF C library through a set of low-level functions that are grouped into a namespace called netcdf. Use the functions in this namespace to read and write data to and from netCDF files. To use the MATLAB netCDF functions effectively, you should be familiar with the netCDF C interface.

Usually, the MATLAB functions in the netcdf namespace correspond directly to routines in the netCDF C library. For example, the MATLAB function netcdf.open corresponds to the netCDF library function nc_open. In some cases, one MATLAB function corresponds to a group of netCDF library functions. For example, instead of creating MATLAB versions of every netCDF library nc_put_att_type function, where type represents a data type, MATLAB uses one function, netcdf.putAtt, to handle all supported data types.

To call one of the functions in the netcdf namespace, you must prefix the function name with the namespace. The syntax of the MATLAB functions is similar to that of the netCDF library functions. However, the netCDF C library functions use input parameters to return data, while their MATLAB counterparts use one or more return values. For example, this is the function signature of the nc_open function in the netCDF library:

int nc_open (const char *path, int omode, int *ncidp); /* C syntax */

The netCDF file identifier is returned in the ncidp argument.

This is the signature of the corresponding MATLAB function, netcdf.open:

ncid = netcdf.open(filename,mode)

Like its netCDF C library counterpart, the MATLAB netCDF function accepts a file name and a constant that specifies the access mode. However, the MATLAB netcdf.open function returns the file identifier, ncid, as a return value.

The MATLAB netCDF functions automatically choose the MATLAB class that best matches the netCDF data type. This table shows the default mapping.

NetCDF Data TypeMATLAB Class
NC_DOUBLEdouble
NC_FLOATsingle
NC_INT64 (NetCDF-4 files only)int64
NC_UINT64 (NetCDF-4 files only)uint64
NC_INTint32
NC_UINT (NetCDF-4 files only)uint32
NC_SHORTint16
NC_USHORT (NetCDF-4 files only)uint16
NC_BYTEint8
NC_UBYTE (NetCDF-4 files only)uint8
NC_CHARchar
NC_STRING (NetCDF-4 files only)string
User-defined NC_VLEN types (NetCDF-4 files only)cell

You can override the default and specify the class of the return data by using an optional argument to the netcdf.getVar function.

Related Topics

External Websites