Main Content

ncwriteatt

Write attribute to NetCDF file

Syntax

ncwriteatt(filename,location,attname,attvalue)
ncwriteatt(filename,location,attname,attvalue,'Datatype',dtype)

Description

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. attvalue can be a character vector, string scalar, string array, or a numeric vector.

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

Input Arguments

filename

Character vector or string scalar specifying the name of a NetCDF file

location

Character vector or string scalar specifying a group or variable in the NetCDF file. To write global attributes, set location to '/' (forward slash).

attname

Character vector or string scalar specifying the name of an existing attribute in a NetCDF file or the name of the attribute that you want to create.

attvalue

Character vector, string scalar, string array, or a numeric vector.

Datatype

Character vector or string scalar specifying the data type to use to write the attribute value attvalue. Specify 'Datatype' as one of these values:

  • 'double' — Write the value of the attribute as type double.

  • 'string' — Write the value of the attribute as type string.

Examples

collapse all

Create a global attribute in a NetCDF file.

copyfile(which('example.nc'),'myfile.nc');
fileattrib('myfile.nc','+w');
ncdisp('myfile.nc');
ncwriteatt('myfile.nc','/','creation_date',datestr(now));
ncdisp('myfile.nc');

Modify an existing attribute in a NetCDF file.

copyfile(which('example.nc'),'myfile.nc');
fileattrib('myfile.nc','+w');
ncdisp('myfile.nc','peaks');
ncwriteatt('myfile.nc','peaks','description','Output of PEAKS');
ncdisp('myfile.nc','peaks');

Write a string array to a dataset attribute in a NetCDF-4 file. Then, display the value of the attribute.

Create a NetCDF-4 file and a variable named Temperature. Then, write the string array ["°​F","°​C"] as the value of the attribute named Units. The array contains non-ASCII string data.

nccreate("myfile.nc", "Temperature", "Format", "netcdf4");
ncwriteatt("myfile.nc", "Temperature", "Units", ["°​F","°​C"])

Display the contents of the variable Temperature.

ncdisp("myfile.nc","Temperature")

Write data to an attribute in a NetCDF-4 file as string data.

Create a NetCDF-4 file and a variable named Temperature. Then, write the character vector 'July' as type NC_STRING to the attribute named Months by specifying the value of "Datatype" as "string". By default, ncwriteatt writes scalar text data as type NC_CHAR.

Create a var

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

Read the value of the attribute Months.

ncreadatt("myfile.nc","Calendar","Months")

Limitations

  • You can create attributes of type NC_STRING using NetCDF-4 files only.

Introduced in R2011a