Main Content


Return NetCDF attribute


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


attrvalue = netcdf.getAtt(ncid,varid,attname) returns attrvalue, the value of the attribute name contained in attname. Specify attname as a character vector or string scalar. When it chooses the data type of attrvalue, MATLAB® attempts to match the NetCDF class of the attribute. For example, if the attribute has the NetCDF data type NC_INT, MATLAB uses the int32 class for the output data. If an attribute has the NetCDF data type NC_BYTE, the class of the output data is int8 value.

attrvalue = netcdf.getAtt(ncid,varid,attname,output_datatype) returns attrvalue, the value of the attribute name specified by attname, using the output class specified by output_datatype. Specify the output data type using 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 =