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_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.

Version History

expand all