Writes an attribute to a NetCDF file.
ncwriteatt(filename, location, attname, attvalue)
Path to a file (string).
Name of the attribute (string).
Path of group or/and variable containing the attribute (string). Full form is /grp/subgrp/.../varname
. For a group attribute, the path must end with a '/'. For a global attribute use '/'.
Attribute value (vector of double/integer or string).
ncwriteatt
writes the attribute specified by attname at the location specified by location in the NetCDF file filename.
The location location is a path to a variable attribute, or a group attribute or a global attribute. The groups that do not exist in the path are created. If the attribute belongs to a variable, this one must be created first (with nccreate or other way).
The attvalue can be a scalar or a vector of numerics, or a string. If the attribute exists, it is overwriten.
If the file does not exist it is created (in the NetCDF4 enhanced format), otherwise it is open in append mode.
filename = fullfile(TMPDIR, 'atts.nc'); // Writes the global attributes 'version' and 'date' ncwriteatt(filename, '/', 'version', int32(1)); ncwriteatt(filename, '/', 'date', now()); // Writes the variable 'grp/precip' nccreate(filename, '/grp/precip', 'Dimensions', list('t', 5)); ncwrite(filename, '/grp/precip', [5.0 8.5 9.5 11.0 7.3]); // Writes the attributes 't' and 'unit' of the variable 'grp/precip' ncwriteatt(filename, '/grp/precip', 't', [0.0 10.0 20.0 30.0 40.0]); ncwriteatt(filename, '/grp/precip', 'unit', 'mm'); // Writes the attribute 'name' of group 'grp' ncwriteatt(filename, 'grp/', 'name', 'some_city'); ncdisp(filename); | ![]() | ![]() |