Main Content

netcdf.putAtt

Write data to 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:

  • Character vector or string scalar that contains one of the following values

    Value of xtypeMATLAB® Class
    NC_DOUBLEdouble
    NC_FLOATsingle
    NC_INT64 (netCDF-4 files only)int64
    NC_UINT64 (netCDF-4 files only)uint64
    NC_INTint32
    NC_UINT (netCDF-4 files only)uint32
    NC_SHORTint16
    NC_USHORT (netCDF-4 files only)uint16
    NC_BYTEint8
    NC_UBYTE (netCDF-4 files only)uint8
    NC_CHARchar
    NC_STRING (netCDF-4 files only)string
  • Equivalent numeric value returned by the netcdf.getConstant function

  • Numeric type identifier returned by the netcdf.defVlen function (for attributes of the user-defined NC_VLEN types that correspond to cell arrays)

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)

Tips

  • If attrvalue has more than one dimension, then the netcdf.putAtt function flattens attrvalue in column-major order before writing the attribute value. For example, specifying attrvalue as [1 2 3; 4 5 6] and specifying attrvalue as [1 4 2 5 3 6] have the same effect.

    Additionally, for variables of type NC_VLEN, if attrvalue contains any entries that have more than one dimension, then the netcdf.putAtt function flattens those entries in column-major order before writing the values. For example, for a variable of type NC_VLEN, specifying attrvalue as

    {[0.5 0.3]; [0 -0.7 5.2; 4.6 2.5 1.8]}

    and specifying attrvalue as

    {[0.5; 0.3] [0; 4.6; -0.7; 2.5; 5.2; 1.8]}

    have the same effect.

Version History

expand all