ncwriteatt - Write attribute to netCDF file - MATLAB (original) (raw)
Write attribute to netCDF file
Syntax
Description
ncwriteatt([filename](#mw%5F6539c421-f989-4589-8d14-5a163ddfb5e5%5Fsep%5Fmw%5Fad64b772-dfed-4501-a67b-d3da0dbe6bc7),[location](#mw%5F016de1ee-7f2b-45b0-abd8-eae6692fa811),[attname](#mw%5F0029189b-68b2-4ffa-811a-cb34466a7f58),[attvalue](#mw%5F0c4eea0b-f9b3-4795-960a-31a7b6f67c78))
creates or modifies the attribute specified by attname
in the group or variable specified by location
in the netCDF file specified byfilename
. 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.
ncwriteatt([filename](#mw%5F6539c421-f989-4589-8d14-5a163ddfb5e5%5Fsep%5Fmw%5Fad64b772-dfed-4501-a67b-d3da0dbe6bc7),[location](#mw%5F016de1ee-7f2b-45b0-abd8-eae6692fa811),[attname](#mw%5F0029189b-68b2-4ffa-811a-cb34466a7f58),[attvalue](#mw%5F0c4eea0b-f9b3-4795-960a-31a7b6f67c78),"Datatype",[dtype](#mw%5F9b143fb3-2282-46f4-9faa-962a2f4792a1))
writes attvalue
as the data type specified bydtype
. For example, specify dtype
as"string"
to write the value in attvalue
as a string.
Examples
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 = '01-Feb-2025 08:34:41'
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 = 1×2 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")
Input Arguments
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 thencwriteatt
function flattens attvalue
in column-major order before writing the attribute value. For example, specifyingattvalue
as [1 2 3; 4 5 6]
and specifyingattvalue
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 dtype | NetCDF 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
The netCDF-related MATLAB functions automatically choose the netCDF data type that best matches the MATLAB data type according to this table.
MATLAB Data Type | NetCDF Data Type |
---|---|
double | NC_DOUBLE |
single | NC_FLOAT |
int32 | NC_INT |
int16 | NC_SHORT |
int8 | NC_BYTE |
char | NC_CHAR |
string scalar | NC_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 sources with formatnetcdf4
.
Version History
Introduced in R2011a
You can write NC_STRING
attributes to netCDF-4 files.