Main Content

netcdf.putVar

Write data to netCDF variable

Syntax

netcdf.putVar(ncid,varid,data)
netcdf.putVar(ncid,varid,start,data)
netcdf.putVar(ncid,varid,start,count,data)
netcdf.putVar(ncid,varid,start,count,stride,data)

Description

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

Note

For variables of type NC_STRING, data can contain UTF-8-encoded characters; for variables of type NC_CHAR, data must contain only ASCII-encoded characters.

ncid is an identifier of a netCDF file, such as returned by netcdf.create or netcdf.open, or an identifier of a netCDF group, such as returned by netcdf.defGrp.

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.

Examples

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('foo.nc','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','NC_DOUBLE',dimid);

Leave define mode and enter data mode to write data.

netcdf.endDef(ncid);

Write data to variable.

netcdf.putVar(ncid,my_varID,my_vardata);

Verify that the variable was created.

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

Close the file.

netcdf.close(ncid)

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

srcFile = fullfile(matlabroot,'toolbox','matlab','demos','example.nc');
copyfile(srcFile,'myfile.nc');
fileattrib('myfile.nc','+w');
ncid = netcdf.open('myfile.nc','WRITE');
varid = netcdf.inqVarID(ncid,'temperature');
data = [100:109];
netcdf.putVar(ncid,varid,0,10,data);
netcdf.close(ncid);

Tips

  • MATLAB® interprets multidimensional data as column-major, but the netCDF C API interprets multidimensional data as row-major. Multidimensional data in the netCDF C API shows dimensions in the reverse of the order shown by MATLAB and consequently appears transposed.

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

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

    and specifying data as

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

    have the same effect.

Version History

Introduced in R2008b

expand all