Writes a variable to a NetCDF file.
ncwrite(filename, varname, vardata, [start, [stride]])
Path to a file (string).
Name of an existing variable (string).
Data to write (scalar, matrix, hypermatrix of double, integer, or string).
Optional indexs at which the data is written (vector of double, indices begin with 1).
Optional inter-element spacing (vector of double).
ncwrite
writes the data in vardata to the variable varname in the NetCDF file filename.
The variable and file must be created first (with nccreate or other way).
The location of the variable (i.e. the path of groups at which the variable is stored) is specified in varname using the form /grp/subgrp/.../varname
.
vardata can be a scalar, a matrix or a hypermatrix of the variable type (if the type of data values differs from the netCDF variable type, type conversion will occur). Variables with unlimited dimension will be extended.
In the case of double/float
variables, missing values or values equal to %nan
are automatically replaced by the fill value during the writing. The fill value is either the value specified in nccreate or if not specified the default value defined by NetCDF.
Indexes at which the data is written are specified using the optional arguments start and stride. If data exists at the same location, it is overwriten.
filename = fullfile(TMPDIR, 'vars.nc'); // Writes the double 'x' 3.2 nccreate(filename, 'x'); ncwrite(filename, 'x', 3.2); // Writes the variable 'n' 100 (int8 scalar) nccreate(filename, 'n', 'Datatype', 'int8'); ncwrite(filename, 'n', 100); // Writes the variable 'y' 1.0 in the group 'grp' nccreate(filename, 'grp/y'); ncwrite(filename, 'grp/y', 1.0); // Writes the string 'Hello' in the variable 'str' nccreate(filename, 'str', 'Datatype', 'char', 'Dimensions', list('len', 10)); ncwrite(filename, 'str', 'Hello'); // Writes an unlimited integer vector 'vec' nccreate(filename, 'vec', 'Datatype', 'int32', 'Dimensions', list('dim', %inf)); ncwrite(filename, 'vec', [1 2 4 8 16]); // Writes a 2D double array mat with dimensions (3, 4) nccreate(filename, 'mat', 'Dimensions', list('r', 3, 'c', 4)); ncwrite(filename, 'mat', [1 2 3 4; 5 6 7 8; 9 10 11 12]); ncdisp(filename); | ![]() | ![]() |