ncwrite

Write data to NetCDF file

Description

example

ncwrite(filename,varname,vardata) writes the text or numeric data in vardata to an existing variable varname in the NetCDF file filename.

The ncwrite function writes the data in vardata starting at the beginning of the variable and, if needed, automatically extends the unlimited dimensions. For more information on unlimited dimensions, see the Dimensions argument of the nccreate function.

example

ncwrite(filename,varname,vardata,start) writes data to an existing variable, beginning at the location specified by start. Use this syntax to append data to an existing variable or to write partial data.

example

ncwrite(filename,varname,vardata,start,stride) writes data with the interval between the indices of each dimension specified by stride.

Examples

collapse all

Create a NetCDF file that contains a variable, and then write data to that variable.

Create a new file myfile.nc containing a variable named pi.

nccreate('myfile.nc','pi');

Write a scalar data with no dimensions to the variable pi.

ncwrite('myfile.nc','pi',3.1416);

Read and display the variable from the file.

valPi = ncread('myfile.nc','pi')
valPi = 3.1416

Write data to a portion of a variable in a NetCDF file starting at a specified location.

Create a file myncfile.nc with an empty 3-by-6 numeric variable vmark. To disable the default fill value for missing or empty variables, set the value of the FillValue name-value pair argument to disable.

nccreate('myncfile.nc','vmark',...
         'Dimensions', {'x',3,'y',6},...
         'FillValue','disable');     

Write a 3-by-3 array to the variable, and then read and display vmark from the file. The ncwrite function writes data starting at the beginning of the variable.

ncwrite('myncfile.nc','vmark',3*eye(3));
varData = ncread('myncfile.nc','vmark');
display(varData)
varData = 3×6

     3     0     0     0     0     0
     0     3     0     0     0     0
     0     0     3     0     0     0

Add another 3-by-3 array to the variable vmark starting at the fourth column of the first row. Next, read and display vmark from the file. The ncwrite function writes the array starting at the location [1 4].

ncwrite('myncfile.nc','vmark',5*eye(3),[1 4]);
varData = ncread('myncfile.nc','vmark');
display(varData)
varData = 3×6

     3     0     0     5     0     0
     0     3     0     0     5     0
     0     0     3     0     0     5

Write data with specified spacing between the variable indices along each dimension.

First, create a file myncfile.nc with an empty 6-by-6 numeric variable vmark. To disable the default fill value for missing or empty variables, set the value of the FillValue name-value pair argument to disable.

nccreate('myncfile.nc','vmark',...
         'Dimensions', {'x',6,'y',6},...
         'FillValue','disable'); 

Next, write a 3-by-3 numeric array to the variable vmark starting at the location [1 1] with a spacing of 2 between the variable indices along each dimension. Read and display vmark from the file.

ncwrite('myncfile.nc','vmark',3*eye(3),[1 1],[2 2]);
varData = ncread('myncfile.nc','vmark');
display(varData)
varData = 6×6

     3     0     0     0     0     0
     0     0     0     0     0     0
     0     0     3     0     0     0
     0     0     0     0     0     0
     0     0     0     0     3     0
     0     0     0     0     0     0

Input Arguments

collapse all

File name of an existing NetCDF file, specified as a character vector or string scalar.

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

Data Types: char | string

Variable name, specified as a character vector or string containing the name of a variable in the NetCDF file.

Data Types: char | string

Variable data, specified as numeric data or text.

Note

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

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.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char

Starting location of the data in the variable, specified as a vector of indices. For an N-dimensional variable, start is a vector of length N containing 1-based indices.

If you do not specify start, then the ncwrite function starts writing the variable from the first index along each dimension.

Data Types: double

Space between the variable indices along each dimension, specified as a numeric vector of integers. For an N-dimensional variable, stride is a vector of length N. The elements of the stride vector correspond, in order, to the variable's dimensions. A value of 1 writes adjacent values of the NetCDF variable in the corresponding dimension. Where as, a value of 2 writes every other value of the NetCDF variable in the corresponding dimension, and so on.

If you do not specify stride, then the ncwrite function writes the data with a default spacing of 1 along each dimension.

Data Types: double

Introduced in R2011a