ncwrite
Write data to NetCDF file
Syntax
Description
ncwrite(
writes the text or numeric data in filename
,varname
,vardata
)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.
Examples
Write Variable to NetCDF File
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 Portion of Variable
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
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
filename
— File name
character vector | string scalar
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
varname
— Variable name
character vector | string scalar
Variable name, specified as a character vector or string containing the name of a variable in the NetCDF file.
Data Types: char
| string
vardata
— Variable data
numeric | text
Variable data, specified as numeric data or text.
Note
For variables of type NC_CHAR
, vardata
must
contain only ASCII-encoded characters. NetCDF-4 files support writing UTF-8 -encoded
characters in variables of type NC_STRING
.
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:
Subtract the value of the
add_offset
attribute fromvardata
.Divide
vardata
by the value of thescale_factor
attribute.Replace any
NaN
invardata
with the value contained in the_FillValue
attribute. If this attribute does not exist, thenncwrite
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
| string
start
— Starting location
vector of ones (default) | numeric vector
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
stride
— Space between variable indices
vector of ones (default) | numeric vector
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
Version History
Apri esempio
Si dispone di una versione modificata di questo esempio. Desideri aprire questo esempio con le tue modifiche?
Comando MATLAB
Hai fatto clic su un collegamento che corrisponde a questo comando MATLAB:
Esegui il comando inserendolo nella finestra di comando MATLAB. I browser web non supportano i comandi MATLAB.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)