Main Content

ncreadatt

Read attribute value from netCDF data source

    Description

    example

    attvalue = ncreadatt(source,location,attname) reads the netCDF attribute attname from the group or variable specified by location in source. The returned attribute value 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 NetCDF to MATLAB Data Type Conversion.

    Examples

    collapse all

    Read the creation_date global attribute of the netCDF file example.nc.

    creationDate = ncreadatt("example.nc","/","creation_date")
    creationDate = 
    '29-Mar-2010'
    

    Read the scale_factor attribute associated with the temperature variable.

    scaleFactor = ncreadatt("example.nc","temperature","scale_factor")
    scaleFactor = 1.8000
    

    Read the description attribute associated with the /grid2 group. You can read group attributes from files with format netcdf4 only.

    groupDescription = ncreadatt("example.nc","/grid2","description")
    groupDescription = 
    'This is another group attribute.'
    

    Input Arguments

    collapse all

    Name of the netCDF data source, specified as a string scalar or character vector. The source argument can be one of these values:

    • The path of a local netCDF source

    • The OPeNDAP URL of a remote OPeNDAP netCDF data source

    • The HTTP URL of a remote netCDF source, with #mode=bytes appended to the end of the URL to enable byte-range reading

      Note

      Byte-range reading is slower than reading from other sources. For more details about byte-range reading, see the netCDF documentation.

    Example: "myNetCDFfile.nc"

    Example: "http://host_name/netcdf_filename#mode=bytes"

    Location of a variable or group in the netCDF data source, specified as a string scalar or character vector. To read a global attribute, set location to "/" (forward slash).

    Example: "myVar"

    Example: "/myGrp/mySubGrp/myNestedVar"

    Example: "myGrp"

    Example: "/myGrp/mySubGrp"

    Attribute name to be read, specified as a string scalar or character vector.

    Example: "myAttribute"

    More About

    collapse all

    NetCDF to MATLAB Data Type Conversion

    The netCDF-related MATLAB functions automatically choose 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

    Introduced in R2011a

    expand all