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

Version History

expand all