Main Content

netcdf.getAtt

Return NetCDF attribute

Syntax

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

Description

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.

Examples

This example opens the example NetCDF file included with MATLAB, example.nc, 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 = netcdf.open("example.nc","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 =

09-Jun-2008

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_DOUBLEdouble
NC_FLOATsingle
NC_INT64 (NetCDF-4 files only)int64
NC_UINT64 (NetCDF-4 files only)uint64
NC_INTint32
NC_UINT (NetCDF-4 files only)uint32
NC_SHORTint16
NC_USHORT (NetCDF-4 files only)uint16
NC_BYTEint8
NC_UBYTE (NetCDF-4 files only)uint8
NC_CHARchar
NC_STRING (NetCDF-4 files only)string
User-defined NC_VLEN types (NetCDF-4 files only)cell

Version History

expand all