ncwriteatt
Write attribute to netCDF file
Syntax
Description
ncwriteatt(
creates or modifies the attribute specified by filename
,location
,attname
,attvalue
)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.
Examples
Create Global Attribute
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 = '05-Sep-2024 15:14:37'
Modify Attribute of Variable
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'
Write Non-ASCII Data as Attribute
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"
Write Attribute with Specified Data Type
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
filename
— Filename
string scalar | character vector
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
— Location of variable or group
string scalar | character vector
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"
attname
— Attribute name
string scalar | character vector
Attribute name to be written, specified as a string scalar or character vector.
Example: "myAttribute"
attvalue
— Attribute value
numeric array | text
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
dtype
— MATLAB data type
"double"
| "single"
| "int32"
| ...
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
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 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 format
netcdf4
.
Version History
Introduced in R2011aR2021b: Write NC_STRING
attributes
You can write NC_STRING
attributes to netCDF-4 files.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)