Main Content

ncwriteatt

Write attribute to netCDF file

    Description

    example

    ncwriteatt(filename,location,attname,attvalue) creates or modifies the attribute specified by attname in the group or variable specified by location in the netCDF file specified by filename. The written attribute value is of the netCDF data type that best matches the MATLAB® data type of attvalue. For more information about how MATLAB determines the best match, see MATLAB to NetCDF Data Type Conversion.

    example

    ncwriteatt(filename,location,attname,attvalue,"Datatype",dtype) writes attvalue as the data type specified by dtype. For example, specify dtype as "string" to write the value in attvalue as a string.

    Examples

    collapse all

    Make a writable local copy of the example.nc file, and examine the value of its global creation_date attribute.

    copyfile(which("example.nc"),"myfile.nc")
    fileattrib("myfile.nc","+w")
    creationDate = ncreadatt("myfile.nc","/","creation_date")
    creationDate = 
    '29-Mar-2010'
    

    Create a modification_date attribute, set it to the current date, and verify its value.

    ncwriteatt("myfile.nc","/","modification_date",datestr(datetime("now")))
    modificationDate = ncreadatt("myfile.nc","/","modification_date")
    modificationDate = 
    '19-Aug-2023 13:57:25'
    

    Make a writable local copy of the example.nc file, and examine the value of the description attribute of the peaks variable.

    copyfile(which("example.nc"),"myfile.nc")
    fileattrib("myfile.nc","+w")
    oldDescription = ncreadatt("myfile.nc","peaks","description")
    oldDescription = 
    'z = peaks(50);'
    

    Update the attribute and verify its new value.

    ncwriteatt("myfile.nc","peaks","description","Output of PEAKS")
    newDescription = ncreadatt("myfile.nc","peaks","description")
    newDescription = 
    'Output of PEAKS'
    

    Make a writable local copy of the example.nc file.

    copyfile(which("example.nc"),"myfile.nc")
    fileattrib("myfile.nc","+w")

    Write the string array ["°F" "°C"] as the value of a new attribute Units of the variable /grid1/temp. The array contains non-ASCII string data, which is supported only for files with format netcdf4.

    ncwriteatt("myfile.nc","/grid1/temp","Units",["°F" "°C"])

    Verify the value of the new attribute.

    ncreadatt("myfile.nc","/grid1/temp","Units")
    ans = 1x2 string
        "°F"    "°C"
    
    

    Create a netCDF-4 format file with a variable named Calendar. Then, write the character vector 'July' as type NC_STRING to the attribute named Months by specifying the data type as "string". By default, ncwriteatt writes scalar text data as type NC_CHAR.

    nccreate("myfile.nc","Calendar","Format","netcdf4")
    ncwriteatt("myfile.nc","Calendar","Months",'July',"Datatype","string")

    Verify the value and data type of the new attribute.

    ncreadatt("myfile.nc","Calendar","Months")
    ans = 
    "July"
    

    Input Arguments

    collapse all

    Filename of an existing netCDF file, specified as a string scalar or character vector.

    If the netCDF file does not exist, then use the nccreate function to create it first.

    Example: "myFile.nc"

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

    Example: "myVar"

    Example: "/myGrp/mySubGrp/myNestedVar"

    Example: "myGrp"

    Example: "/myGrp/mySubGrp"

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

    Example: "myAttribute"

    Attribute value, specified as a numeric array or text.

    Note

    If attvalue has more than one dimension, then the ncwriteatt function flattens attvalue in column-major order before writing the attribute value. For example, specifying attvalue as [1 2 3; 4 5 6] and specifying attvalue as [1 4 2 5 3 6] have the same effect.

    Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | char | string

    MATLAB data type to use when writing the attribute, specified as one of the values in this table. When ncwriteatt creates or modifies the attribute in the netCDF file, it uses the corresponding netCDF data type.

    Value of dtypeNetCDF Attribute Type
    "double"NC_DOUBLE
    "single"NC_FLOAT
    "int32"NC_INT
    "int16"NC_SHORT
    "int8"NC_BYTE
    "char"NC_CHAR
    "int64" (*)NC_INT64
    "uint64" (*)NC_UINT64
    "uint32" (*)NC_UINT
    "uint16" (*)NC_USHORT
    "uint8" (*)NC_UBYTE
    "string" (*)NC_STRING

    (*) These values of dtype are available only for files with format netcdf4.

    Example: "int16"

    Data Types: string | char

    More About

    collapse all

    MATLAB to NetCDF Data Type Conversion

    The netCDF-related MATLAB functions automatically choose the netCDF data type that best matches the MATLAB data type according to this table.

    MATLAB Data TypeNetCDF Data Type
    doubleNC_DOUBLE
    singleNC_FLOAT
    int32NC_INT
    int16NC_SHORT
    int8NC_BYTE
    charNC_CHAR
    string scalarNC_CHAR
    int64 (*)NC_INT64
    uint64 (*)NC_UINT64
    uint32 (*)NC_UINT
    uint16 (*)NC_USHORT
    uint8 (*)NC_UBYTE
    string vector (*)NC_STRING

    (*) These MATLAB data types are available only for files with format netcdf4.

    Version History

    Introduced in R2011a

    expand all