Skip to Main Content Skip to Search
Product Documentation

ncwrite - Write data to NetCDF file

Syntax

ncwrite(filename,varname,vardata)
ncwrite(filename,varname,vardata,start,stride)

Description

ncwrite(filename,varname,vardata) writes the numerical or char data in vardata to an existing variable varname in the NetCDF file filename. ncwrite writes the data in vardata starting at the beginning of the variable and extends unlimited dimensions automatically, if needed.

If the NetCDF file or the variable do not exist, use nccreate to create them first.

ncwrite(filename,varname,vardata,start,stride) writes vardata to an existing variable varname in file filename beginning at the location given by start. stride is an optional argument that specifies the inter-element spacing of the data written. Use this syntax to append data to an existing variable or write partial data.

Tips

Input Arguments

filename

Text string specifying the name of a NetCDF file. If the file does not exist, use nccreate to create it first.

varname

Text string specifying the name of a variable in a NetCDF file. If the variable does not exist, use nccreate to create it first.

vardata

Data to write to the variable in the NetCDF file.

start

For an N-dimensional variable, start is a vector of indices of length N specifying the starting location. Indices are 1-based.

stride

(Optional) Vector of length N, specifying the inter-element spacing.

Default: Vector of ones

Examples

Create a new netcdf4_classic file, and write a scalar variable with no dimensions. Add the creation time as a global attribute.

nccreate('myfile.nc','pi');
ncwrite('myfile.nc','pi',3.1);
ncwriteatt('myfile.nc','/','creation_time',datestr(now));
% overwrite existing data
ncwrite('myfile.nc','pi',3.1416);
ncdisp('myfile.nc');
 

Create a netcdf4_classic file with a variable defined on an unlimited dimension. Write data incrementally to the variable.

nccreate('myncfile.nc','vmark',...
         'Dimensions', {'time', inf, 'cols', 6},...
         'ChunkSize',  [3 3],...
         'DeflateLevel', 2);
ncwrite('myncfile.nc','vmark', eye(3),[1 1]);
varData = ncread('myncfile.nc','vmark');
disp(varData);
ncwrite('myncfile.nc','vmark',fliplr(eye(3)),[1 4]);
varData = ncread('myncfile.nc','vmark');
disp(varData);   

See Also

nccreate | ncdisp | ncinfo | ncread | ncwriteatt | netcdf

Tutorials

  


» Learn more
» Download free kit
» Get trial software

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS