Main Content


Return NetCDF attribute


attrvalue = netcdf.getAtt(ncid,varid,attname)
attrvalue = netcdf.getAtt(ncid,varid,attname,output_type)


attrvalue = netcdf.getAtt(ncid,varid,attname) returns attrvalue, the value of the attribute name specified by attname. Specify attname as a string scalar or character vector. The returned attribute value attrvalue is of the MATLAB® data type that best matches the NetCDF data type of attname. For more information about how MATLAB determines the best match, see More About.

attrvalue = netcdf.getAtt(ncid,varid,attname,output_type) returns attrvalue using the output data type specified by output_type. Specify output_type as one of these values:

  • "double"

  • "single"

  • "int64"

  • "uint64"

  • "int32"

  • "uint32"

  • "int16"

  • "uint16"

  • "int8"

  • "uint8"

  • "char"

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


This example opens the example NetCDF file included with MATLAB,, and gets the value of the attribute associated with the first variable. The example also gets the value of the global variable in the file.

% Open a NetCDF file.
ncid ="","NC_NOWRITE");

% Get name of first variable.
[varname vartype vardimIDs varatts] = netcdf.inqVar(ncid,0);

% Get ID of variable, given its name.
varid = netcdf.inqVarID(ncid,varname);

% Get attribute name, given variable id.
attname = netcdf.inqAttName(ncid,varid,0);

% Get value of attribute.
attval = netcdf.getAtt(ncid,varid,attname);

% Get name of global attribute
gattname = netcdf.inqAttName(ncid,netcdf.getConstant("NC_GLOBAL"),0);

% Get value of global attribute.
gattval = netcdf.getAtt(ncid,netcdf.getConstant("NC_GLOBAL"),gattname)

gattval =


More About

collapse all

NetCDF to MATLAB Data Type Conversion

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

NetCDF Data TypeMATLAB Data Type
NC_INT64 (NetCDF-4 files only)int64
NC_UINT64 (NetCDF-4 files only)uint64
NC_UINT (NetCDF-4 files only)uint32
NC_USHORT (NetCDF-4 files only)uint16
NC_UBYTE (NetCDF-4 files only)uint8
NC_STRING (NetCDF-4 files only)string
User-defined NC_VLEN types (NetCDF-4 files only)cell

Version History

expand all