Map NetCDF API Syntax to MATLAB Syntax
MATLAB® provides access to the routines in the NetCDF C library through a set
of low-level functions that are grouped into a package called
netcdf
. Use the functions in this package 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
package correspond directly to
routines in the NetCDF C library. For example, the MATLAB function netcdf.open
corresponds to the NetCDF library
routine 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_
function, where
type
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
package, you must prefix
the function name with the package name. The syntax of the MATLAB functions is similar to the NetCDF library routines. However, the NetCDF C
library routines 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
routine 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 Type | MATLAB Class |
---|---|
NC_DOUBLE | double |
NC_FLOAT | single |
NC_INT64 (NetCDF-4 files only) | int64 |
NC_UINT64 (NetCDF-4 files only) | uint64 |
NC_INT | int32 |
NC_UINT (NetCDF-4 files only) | uint32 |
NC_SHORT | int16 |
NC_USHORT (NetCDF-4 files only) | uint16 |
NC_BYTE | int8 |
NC_UBYTE (NetCDF-4 files only) | uint8 |
NC_CHAR | char |
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.