This example creates the NetCDF file named 'foo.nc', but only if
the file does not already exist. The returned value is a "netcdf"
object, which represents the file in all subsequent transactions.
If an error occurs, the empty-matrix [] is returned.
nc = netcdf('foo.nc', 'noclobber')
if isempty(nc), error('## Bad netcdf operation.'), end
This example opens the NetCDF file named 'foo.nc' for read-only access.
The returned value is a "netcdf" object, which represents the file in
all subsequent transactions. If an error occurs, the empty-matrix []
is returned.
nc = netcdf('foo.nc', 'nowrite')
if isempty(nc), error('## Bad netcdf operation.'), end
This example opens the NetCDF file named 'foo.nc' for read/write
access, then puts it into "define" mode. If an error occurs, the
empty-matrix [] is returned. Otherwise, the same "netcdf" object
is returned.
nc = netcdf('foo.nc', 'write')
if isempty(nc), error(' ## Bad netcdf operation.'), end
result = redef(nc)
if isempty(result), error('## Bad redef operation.'), end
This example opens the NetCDF file named 'foo.nc' for read/write
access, puts it into "define" mode, then takes it out of "define" mode.
If an error occurs, the empty-matrix [] is returned. Otherwise, the
same "netcdf" object is returned.
nc = netcdf('foo.nc', 'write')
if isempty(nc), error(' ## Bad netcdf operation.'), end
result = redef(nc)
result = endef(nc)
if isempty(result), error('## Bad endef operation.'), end
This example opens, then closes, the NetCDF file named 'foo.nc'.
If an error occurs, the "netcdf" object is returned. Otherwise, the
empty-matrix [] is returned.
nc = netcdf('foo.nc', 'write')
if isempty(nc), error(' ## Bad netcdf operation.'), end
result = close(nc)
if ~isempty(result), error('## Bad close operation.'), end
This example opens the NetCDF file named 'foo.nc', then inquires
about the objects in it.
nc = netcdf('foo.nc', 'write')
if isempty(nc), error(' ## Bad netcdf operation.'), end
theDims = dim(nc) % List of "ncdim" dimension objects.
theVars = var(nc) % List of "ncvar" variable objects.
theGAtts = att(nc) % List of "ncatt" global attribute objects.
theRecdim = recdim(nc) % The "ncdim" record-dimension object.
This example opens, synchronizes, then closes, the NetCDF file named 'foo.nc'.
If an error occurs, the "netcdf" object is returned. Otherwise, the
empty-matrix [] is returned.
nc = netcdf('foo.nc', 'write')
if isempty(nc), error('## Bad netcdf operation.'), end
result = sync(nc)
if ~isempty(result), error('## Bad sync operation.'), end
This example opens, enters "define" mode, then aborts, the NetCDF file named 'foo.nc'. If an error occurs, the "netcdf" object is returned. Otherwise, the empty-matrix [] is returned.
nc = netcdf('foo.nc', 'write')
if isempty(nc), error('## Bad netcdf operation.'), end
result = redef(nc)
result = abort(nc)
if ~isempty(result), error('## Bad abort operation.'), end
This example opens the NetCDF file named 'foo.nc', then sets the
fillmode for "nofill". If an error occurs, the empty-matrix []
is returned. Otherwise, the same "netcdf" object is returned.
nc = netcdf('foo.nc', 'write')
if isempty(nc), error('## Bad netcdf operation.'), end
result = setfill(nc, 0)
if isempty(result), error('## Bad setfill operation.'), end
This example opens the NetCDF file named 'foo.nc', then adds
an "unlimited" dimension named 'time'. If an error occurs, the
empty-matrix [] is returned.
nc = netcdf('foo.nc', 'write')
if isempty(nc), error('## Bad netcdf operation.'), end
nc('time') = 0
if isempty(nc('time')), error(' ## Bad dimdef operation.'), end
This example opens the NetCDF file named 'foo.nc',
then gets its 'time' dimension. If an error occurs,
the empty-matrix [] is returned.
nc = netcdf('foo.nc', 'write')
if isempty(nc), error('## Bad netcdf operation.'), end
theDim = nc('time')
if isempty(theDim), error(' ## No such dimension.'), end
This example opens the NetCDF file named 'foo.nc', then
gets information about its 'time' dimension. If an error
occurs, the empty-matrix [] is returned.
nc = netcdf('foo.nc', 'write')
if isempty(nc), error('## Bad netcdf operation.'), end
theDim = nc('time');
if isempty(theDimid), error(' ## Bad ncdim operation.'), end
theDimname = name(theDim)
theDimsize = size(theDim)
theDimsize = theDim(:)
nc = parent(theDim)
This example opens the NetCDF file named 'foo.nc', then
renames its 'time' dimension to 'newtime'. If an error
occurs, the empty-matrix [] is returned.
nc = netcdf('foo.nc', 'write')
if isempty(nc), error('## Bad netcdf operation.'), end
result = name(nc('time'), 'newtime')
if isempty(result), error(' ## Bad name operation.'), end
This example opens the NetCDF file named 'foo.nc', then creates
the single-precision variable "elevation(latitude, longitude)", where
latitude and longitude are dimensions, as well as coordinate-variables.
If an error occurs, the empty-matrix [] is returned. The variables
are populated using colon-syntax (:), which denotes all the elements
in an array. The alternative is to use regular array indexing.
nc = netcdf('foo.nc', 'write')
if isempty(nc), error('## Bad netcdf operation.'), end
nc('latitude') = 19
nc('longitude') = 36
nc{latitude'} = ncfloat('latitude')
nc{longitude'} = ncfloat('longitude')
nc{'elevation'} = ncfloat('latitude', 'longitude')
if isempty(nc{'elevation'}), error(' ## Bad vardef operation.'), end
nc{'latitude'}(:) = -90:10:90
nc{'longitude'}(:) = 0:10:350
nc{'elevation'}(:) = 0 % All zeros.
close(nc)
This example opens the NetCDF file named 'foo.nc',
then gets its 'elevation' variable. If an error occurs,
the empty-matrix [] is returned.
nc = netcdf('foo.nc', 'write')
if isempty(nc), error('## Bad netcdf operation.'), end
theVar = nc{'elevation'}
if isempty(theVar), error(' ## No such variable.'), end
This example opens the NetCDF file named 'foo.nc', then
gets information about its 'elevation' variable. If an error
occurs, the empty-matrix [] is returned.
nc = netcdf('foo.nc', 'write')
if isempty(nc), error('## Bad netcdf operation.'), end
theVar = nc{'elevation'};
if isempty(theVar), error(' ## Bad ncvar operation.'), end
theVarname = name(theVar)
theVarsize = size(theVar)
theVarsize = theVar(:)
nc = parent(theVar)
This example opens the NetCDF file named 'foo.nc', then
writes some random values to its "elevation(latitude, longitude)"
variable in several different ways.
nc = netcdf('foo.nc', 'write')
if isempty(nc), error('## Bad netcdf operation.'), end
theVar = nc{'elevation'};
if isempty(theVar), error(' ## Bad ncvar operation.'), end
theVar(1:10, 1:20) = rand(10, 20);
theVar(1:10, :) = rand(10, size(nc('longitude')));
theVar(:) = rand(size(theVar));
This example opens the NetCDF file named 'foo.nc', then
reads some random values from its "elevation(latitude, longitude)"
variable in several different ways.
nc = netcdf('foo.nc', 'write')
if isempty(nc), error('## Bad netcdf operation.'), end
theVar = nc{'elevation'};
if isempty(theVar), error(' ## Bad ncvar operation.'), end
theValues = theVar(1:10, 1:20);
theValues = theVar(1:10, :);
theValues = theVar(:);
rcode)
This example opens the NetCDF file named 'foo.nc', then
renames its 'elevation' variable to 'height'. If an error
occurs, the empty-matrix [] is returned.
nc = netcdf('foo.nc', 'write')
if isempty(nc), error('## Bad netcdf operation.'), end
result = name(nc{'elevation'}, 'height')
if isempty(result), error(' ## Bad name operation.'), end
This example opens the NetCDF file named 'foo.nc', then adds
a "description" attributes to the file and some "units" to the 'elevation'
variable.
nc = netcdf('foo.nc', 'write')
if isempty(nc), error('## Bad netcdf operation.'), end
nc.description = ncchar('This file is an example.')
nc.description = 'This file is an example.'
nc{'elevation'}.units = ncchar('meters')
nc{'elevation'}.units = 'meters'
This example opens the NetCDF file named 'foo.nc', then reads
the "description" global attribute and the "units" attribute of the 'elevation'
variable.
nc = netcdf('foo.nc', 'write')
if isempty(nc), error('## Bad netcdf operation.'), end
theDescription = nc.description(:)
theUnits =nc{'elevation'}.units(:)
This example opens the NetCDF file named 'foo.nc', then
gets information about its 'description' global attribute. If an error
occurs, the empty-matrix [] is returned.
nc = netcdf('foo.nc', 'write')
if isempty(nc), error('## Bad netcdf operation.'), end
theAtt = nc.description;
if isempty(theAtt), error(' ## Bad ncatt operation.'), end
theAttname = name(theAtt)
theAttsize = size(theAtt)
'theAtttype' = datatype(theAtt)
theParent = parent(theAtt)
This example opens the NetCDF file named 'foo.nc' and copies
the "units" attribute of the "latitude" variable to the "longitude"
variable. If an error occurs, the empty-matrix [] is returned.
nc = netcdf('foo.nc', 'write')
if isempty(nc), error('## Bad netcdf operation.'), end
nc{'longitude'} < nc{'latitude'}.units
This example opens the NetCDF file named 'foo.nc' and deletes
the "description" global attribute. If an error occurs, the empty-matrix
[] is returned.
nc = netcdf('foo.nc', 'write')
if isempty(nc), error('## Bad netcdf operation.'), end
nc.description = [];