This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.


Write netCDF attribute




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

    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. See netcdf for more information.


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('','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.

% Write data to variable.

% Re-enter define mode.

% Create an attribute associated with the variable.

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

xtype =


xlen =


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

ncid = netcdf.create('','CLOBBER');
varid = netcdf.getConstant('GLOBAL');
Was this topic helpful?