Main Content

netcdf.getVar

Read data from netCDF variable

Syntax

data = netcdf.getVar(ncid,varid)
data = netcdf.getVar(ncid,varid,start)
data = netcdf.getVar(ncid,varid,start,count)
data = netcdf.getVar(ncid,varid,start,count,stride)
data = netcdf.getVar(___,output_type)

Description

data = netcdf.getVar(ncid,varid) returns data, the value of the variable specified by varid. ncid is a NetCDF file identifier returned by netcdf.create or netcdf.open. The returned value data is of the MATLAB® data type that best matches the NetCDF data type of the variable specified by varid. For more information about how MATLAB determines the best match, see More About.

Note

If the variable specified by varid is of type NC_STRING, then it can contain UTF-8-encoded characters; if the variable specified by varid is of type NC_CHAR, then it must contain only ASCII-encoded characters.

data = netcdf.getVar(ncid,varid,start) returns a single value starting at the specified index, start.

data = netcdf.getVar(ncid,varid,start,count) returns a contiguous section of a variable. start specifies the starting point and count specifies the amount of data to return.

data = netcdf.getVar(ncid,varid,start,count,stride) returns a subset of a section of a variable. start specifies the starting point, count specifies the extent of the section, and stride specifies which values to return.

data = netcdf.getVar(___,output_type) specifies the data type of the return value data. Specify output_type as one of these values:

  • "double"

  • "single"

  • "int64"

  • "uint64"

  • "int32"

  • "uint32"

  • "int16"

  • "uint16"

  • "int8"

  • "uint8"

  • "char"

This function corresponds to several functions in the NetCDF library C API. To use this function, you should be familiar with the NetCDF programming paradigm.

Examples

collapse all

Open the example file, example.nc.

ncid = netcdf.open('example.nc','NC_NOWRITE');

Get the name of the first variable in the file.

varname = netcdf.inqVar(ncid,0)
varname = 
'avagadros_number'

Get variable ID of the first variable, given its name.

varid = netcdf.inqVarID(ncid,varname)
varid = 
0

Get the value of the variable. Use the variable ID as the second input to the netcdf.getVar function.

data = netcdf.getVar(ncid,varid)
data = 
6.0221e+23

Display the data type of the output value.

whos data
  Name      Size            Bytes  Class     Attributes

  data      1x1                 8  double              

Get the value of the avogadros_number variable again, specifying that the output data type should be single.

data = netcdf.getVar(ncid,varid,'single');

Display the data type of the output value.

whos data
  Name      Size            Bytes  Class     Attributes

  data      1x1                 4  single              

Close the NetCDF file.

netcdf.close(ncid)

More About

collapse all

NetCDF to MATLAB Data Type Conversion

When the optional input output_type is not specified, netcdf.getVar automatically chooses the MATLAB data type that best matches the NetCDF data type according to this table.

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

(*) These netCDF data types are available only for files with format netcdf4.

Tips

  • MATLAB interprets data as column major, but the netCDF C API interprets data as row major. Multidimensional data in the netCDF C API shows dimensions in the reverse of the order shown by MATLAB and consequently appears transposed.

Version History

Introduced in R2008b

expand all