Main Content

netcdf.putAtt

Write NetCDF attribute

Syntax

netcdf.putAtt(ncid,varid,attrname,attrvalue)
netcdf.putAtt(ncid,varid,attrname,attrvalue,xtype)

Description

netcdf.putAtt(ncid,varid,attrname,attrvalue) writes the attribute named attrname with value attrvalue to the NetCDF variable specified by varid. To specify a global attribute, use netcdf.getConstant('NC_GLOBAL') for varid.

ncid is a NetCDF file identifier returned by netcdf.create or netcdf.open.

netcdf.putAtt(ncid,varid,attrname,attrvalue,xtype) writes attrvalue as the data type specified in xtype. Specify the value of xtype as a character vector or string scalar containing one of the following values, or as the equivalent numeric value returned by the netcdf.getConstant function:

Value of xtypeMATLAB® Class
NC_DOUBLE'double'
NC_FLOAT'single'
NC_INT64 (NetCDF-4 files only)'int64'
NC_UINT64 (NetCDF-4 files only)'uint64'
NC_INT'int32'
NC_UINT (NetCDF-4 files only)'uint32'
NC_SHORT'int16'
NC_USHORT (NetCDF-4 files only)'uint16'
NC_BYTE'int8'
NC_UBYTE (NetCDF-4 files only)'uint8'
NC_CHAR'char'
NC_STRING (NetCDF-4 files only)'string'

Note

You cannot use netcdf.putAtt to set the '_FillValue' attribute of NetCDF4 files. Use the netcdf.defVarFill function to set the fill value for a variable.

The netcdf.putAtt function corresponds to several attribute I/O functions in the NetCDF library C API. To use this function, you should be familiar with the NetCDF programming paradigm.

Examples

collapse all

This example creates a new NetCDF file, defines a dimension and a variable, adds data to the variable, and then creates an attribute associated with the variable. To run this example, you must have writer permission in your current directory.

% Create a variable in the workspace.
my_vardata = linspace(0,50,50);

% Create a netCDF file.
ncid = netcdf.create('foo.nc','NC_WRITE');

% Define a dimension in the file.
dimid = netcdf.defDim(ncid,'my_dim',50);
 
% Define a new variable in the file.
varid = netcdf.defVar(ncid,'my_var','double',dimid);

% Leave define mode and enter data mode to write data.
netcdf.endDef(ncid);

% Write data to variable.
netcdf.putVar(ncid,varid,my_vardata);

% Re-enter define mode.
netcdf.reDef(ncid);

% Create an attribute associated with the variable.
netcdf.putAtt(ncid,0,'my_att',10);

% Verify that the attribute was created.
[xtype xlen] = netcdf.inqAtt(ncid,0,'my_att')

xtype =

     6


xlen =

     1

This example creates a new NetCDF file, specifies a global attribute, and assigns a value to the attribute.

ncid = netcdf.create('myfile.nc','CLOBBER');
varid = netcdf.getConstant('GLOBAL');
netcdf.putAtt(ncid,varid,'creation_date',datestr(now));
netcdf.close(ncid);

Write a string array as type NC_STRING to a global attribute in a NetCDF-4 file. Then, return the value of the global attribute.

Create a NetCDF-4 file, and write the string array ["°​F","°​C"] as the value of the global attribute Units.

ncid = netcdf.create("myfile.nc","NETCDF4");
netcdf.putAtt(ncid, netcdf.getConstant("NC_GLOBAL"),"Units", ["°​F","°​C"])
netcdf.close(ncid)

Return the value of the global attribute, then close the NetCDF-4 file.

ncid = netcdf.open("myfile.nc");
netcdf.getAtt(ncid, netcdf.getConstant("NC_GLOBAL"), "Units")
netcdf.close(ncid)

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

Create a NetCDF-4 file. Then, write the character vector 'March' as type 'NC_STRING' to the global attribute Month.

ncid = netcdf.create("myfile.nc", "NETCDF4");
netcdf.putAtt(ncid, netcdf.getConstant("NC_GLOBAL"),...
      "Month", 'March', 'NC_STRING')
netcdf.close(ncid)

Return the value of the global attribute Month, then close the NetCDF-4 file.

ncid = netcdf.open("myfile.nc");
netcdf.getAtt(ncid, netcdf.getConstant("NC_GLOBAL"), "Month")
netcdf.close(ncid)