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 data to netCDF variable




netcdf.putVar(ncid,varid,data) writes data to a netCDF variable identified by varid.

    Note:   For variable values containing text data, the data input must have only ASCII encoded characters.

ncid is a netCDF file identifier returned by netcdf.create or

netcdf.putVar(ncid,varid,start,data) writes a single data value into the variable at the index specified by start.

netcdf.putVar(ncid,varid,start,count,data) writes a section of values into the netCDF variable at the index specified by the vector start to the extent specified by the vector count, along each dimension of the specified variable.

netcdf.putVar(ncid,varid,start,count,stride,data) writes the subsection specified by sampling interval, stride, of the values in the section of the variable beginning at the index start and to the extent specified by count.

This function corresponds to several variable 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.


collapse all

Create a new netCDF file and write a variable to the file.

Create a 50 element vector for a variable.

my_vardata = linspace(0,50,50);

Open the netCDF file.

ncid = netcdf.create('','NOCLOBBER');

Define the dimensions of the variable.

dimid = netcdf.defDim(ncid,'my_dim',50);

Define a new variable in the file.

my_varID = netcdf.defVar(ncid,'my_var','double',dimid);

Leave define mode and enter data mode to write data.


Write data to variable.


Verify that the variable was created.

[varname xtype dimid natts ] = netcdf.inqVar(ncid,0)
varname = 
xtype = 6
dimid = 0
natts = 0

Close the file.


Write to the first ten elements of the example temperature variable.

srcFile = fullfile(matlabroot,'toolbox','matlab','demos','');
ncid ='','WRITE');
varid = netcdf.inqVarID(ncid,'temperature');
data = [100:109];
Was this topic helpful?