netcdf.putAtt

Write netCDF attribute

Syntax

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

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.

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.

This 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

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);