ncwrite - Write data to netCDF file - MATLAB (original) (raw)

Write data to netCDF file

Syntax

Description

ncwrite([filename](#bsu%5Fi%5F6-1%5Fsep%5Fmw%5Fad64b772-dfed-4501-a67b-d3da0dbe6bc7),[varname](#mw%5Fadc8e4ec-fdfb-48e8-ae75-6e46d196d1ec),[vardata](#mw%5Fcd303a0f-c0ca-4710-8613-8b812f1e825c)) writes the text or numeric data in vardata to an existing variablevarname in the netCDF file filename.

The ncwrite function writes the data invardata 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](#bsu%5Fi%5F6-1%5Fsep%5Fmw%5Fad64b772-dfed-4501-a67b-d3da0dbe6bc7),[varname](#mw%5Fadc8e4ec-fdfb-48e8-ae75-6e46d196d1ec),[vardata](#mw%5Fcd303a0f-c0ca-4710-8613-8b812f1e825c),[start](#mw%5Ff2268a8a-a1ca-4638-b0e3-da31a9a6648a)) writes data to an existing variable, beginning at the location specified bystart. Use this syntax to append data to an existing variable or to write partial data.

example

ncwrite([filename](#bsu%5Fi%5F6-1%5Fsep%5Fmw%5Fad64b772-dfed-4501-a67b-d3da0dbe6bc7),[varname](#mw%5Fadc8e4ec-fdfb-48e8-ae75-6e46d196d1ec),[vardata](#mw%5Fcd303a0f-c0ca-4710-8613-8b812f1e825c),[start](#mw%5Ff2268a8a-a1ca-4638-b0e3-da31a9a6648a),[stride](#mw%5F705173a7-8ef2-4ec0-b7b6-f2e5015089c1)) writes data with the interval between the indices of each dimension specified bystride.

example

Examples

collapse all

Create a file myfile.nc containing a variable named c.

nccreate("myfile.nc","c")

Write a scalar value to the variable.

ncwrite("myfile.nc","c",299792458)

Read and display the variable from the file.

speedOfLight = ncread("myfile.nc","c")

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

nccreate("myfile.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("myfile.nc","vmark",3*eye(3)) varData = ncread("myfile.nc","vmark")

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. The ncwrite function writes the array starting at the location [1 4].

ncwrite("myfile.nc","vmark",5*eye(3),[1 4]) varData = ncread("myfile.nc","vmark")

varData = 3×6

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

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

nccreate("myfile.nc","vmark", ... "Dimensions", {"x",6,"y",6},"FillValue","disable")

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.

ncwrite("myfile.nc","vmark",3*eye(3),[1 1],[2 2]) varData = ncread("myfile.nc","vmark")

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

Filename of an existing netCDF file, specified as a string scalar or character vector.

If the netCDF file does not exist, then use the nccreate function to create it first.

Example: "myFile.nc"

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

If filename specifies a file with formatnetcdf4, you can specify the location of the variable within the group hierarchy by specifying varname as a fully qualified name.

If the variable does not exist, then use the nccreate function to create it first.

Example: "myVar"

Example: "/myGrp/mySubGrp/myNestedVar"

Variable data, specified as a numeric array or text. The value ofvardata must be compatible with data type of the netCDF variablevarname.

If the variable varname has attributes_FillValue, scale_factor, oradd_offset, then the ncwrite function expects the data to be compatible with the double data type. To castvardata into the netCDF data type, thencwrite function applies these attribute conventions in a sequence before writing vardata:

  1. If the add_offset attribute exists, thenncwrite subtracts the value of theadd_offset attribute from the values invardata.
  2. If the scale_factor attribute exists, thenncwrite divides the values in vardata by the value of the scale_factor attribute.
  3. If the _FillValue attribute exists, thenncwrite replaces NaN vardata values with values equal to that of the_FillValue attribute.

Note

For variables of type NC_STRING, vardata can contain UTF-8-encoded characters; for variables of typeNC_CHAR, vardata must contain only ASCII-encoded characters.

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

Starting location of the data in the variable, specified as a numeric vector of positive integers. For an _N_-dimensional variable, specifystart as a vector of length N containing 1-based indices.

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

Example: [2 1 3]

Data Types: double

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

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

Example: [2 10 1]

Data Types: double

Tips

Version History

Introduced in R2011a

expand all

You can write NC_STRING data to netCDF-4 files.