This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.


Write data to NetCDF file




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.

Input Arguments


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


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


Data to write to the variable in the NetCDF file.


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


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


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

Default: Vector of ones


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

% overwrite existing data

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

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


  • If the variable varname exists, then ncwrite expects the data type of vardata to match the NetCDF variable data type.

  • If the variable varname has attributes _FillValue, scale_factor, or add_offset, then the ncwrite function expects the data to be of the data type double. To cast vardata into the NetCDF data type, the ncwrite function applies these attribute conventions in a sequence:

    1. Subtract the value of the add_offset attribute from vardata.

    2. Divide vardata by the value of the scale_factor attribute.

    3. Replace any NaN in vardata with the value contained in the _FillValue attribute. If this attribute does not exist, then ncwrite uses the fill value for this variable as specified by the NetCDF library.

Introduced in R2011a

Was this topic helpful?