nccreate
Create variable in netCDF file
Description
nccreate(
creates a variable with additional options specified by one or more name-value
arguments. For example, to create a nonscalar variable, use the
filename
,varname
,Name,Value
)Dimensions
name-value argument.
Examples
Create New Variables in NetCDF File
Create a netCDF file named myexample.nc
that
contains a variable named Var1
.
nccreate('myexample.nc','Var1')
Create a second variable in the same file.
nccreate('myexample.nc','Var2')
Display the contents of the netCDF file.
ncdisp('myexample.nc')
Source: pwd\myexample.nc Format: netcdf4_classic Variables: Var1 Size: 1x1 Dimensions: Datatype: double Var2 Size: 1x1 Dimensions: Datatype: double
Create Variable and Specify Dimensions and File Format
Create a new two-dimensional variable named peaks
in
a classic (netCDF-3) format file named myncclassic.nc
.
Use the "Dimensions"
name-value argument to
specify the name and length of each dimension. Use the "Format"
name-value
argument to specify the file format.
nccreate("myncclassic.nc","peaks",... "Dimensions",{"r",300,"c",400},"Format","classic");
Write data to the variable.
ncwrite("myncclassic.nc","peaks",peaks(100));
Display the contents of the netCDF file.
ncdisp("myncclassic.nc");
Source: pwd\myncclassic.nc Format: classic Dimensions: r = 300 c = 400 Variables: peaks Size: 300x400 Dimensions: r,c Datatype: double
Input Arguments
filename
— File name
character vector | string scalar
File name, specified as a character vector or string scalar. The file is an existing netCDF file, or the name you want to assign to a new netCDF file.
Example: 'myFile.nc'
varname
— Name of new variable
character vector | string scalar
Name of the new variable, specified as a character vector or string scalar.
Example: 'myVar'
Name-Value Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN
, where Name
is
the argument name and Value
is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Before R2021a, use commas to separate each name and value, and enclose
Name
in quotes.
Example: nccreate('myFile.nc','Var1','Datatype','double','Format','classic')
creates
a variable named Var1
of type NC_DOUBLE
in
a netCDF-3 file named myFile.nc
.
Dimensions
— Dimensions of variable
cell array
Dimensions of the new variable, specified as the comma-separated pair consisting of
'Dimensions'
and a cell array. The cell array
lists the dimension name as a character vector or string scalar followed
by its numerical length, in this form:
{dname1,dlength1,dname2,dlength2, ...}
. The
dname1
input is the name of the first dimension
specified as a character vector or string scalar,
dlength1
is the length of the first dimension,
dname2
is the name of the second dimension, and
so on. If a dimension exists, specifying its length is optional. A
variable with a single dimension is always treated as a column
vector.
Use Inf
to specify an unlimited dimension.
A netcdf4
format file can have any number of unlimited
dimensions in any order. All other formats can have only one unlimited
dimension per file and it must be specified last in the cell array.
nccreate
creates the dimension at the same
location as the variable. For netcdf4
format files,
you can specify a different location for the dimension using a fully
qualified dimension name.
Example: 'Dimensions',{'dim1',100,'dim2',150,'dim3',Inf}
Datatype
— MATLAB® data type
'double'
(default) | character vector | string scalar
MATLAB data type, specified as the comma-separated pair
consisting of 'Datatype'
and a character vector or
string scalar containing the name of the data type. When
nccreate
creates the variable in the netCDF
file, it uses a corresponding netCDF datatype. This table lists valid
values for 'Datatype'
and the corresponding netCDF
variable type that nccreate
creates.
Value of Datatype | NetCDF Variable Type |
---|---|
'double' | NC_DOUBLE |
'single' | NC_FLOAT |
'int64' | NC_INT64 (netCDF-4 files only) |
'uint64' | NC_UINT64 (netCDF-4 files only) |
'int32' | NC_INT |
'uint32' | NC_UINT (netCDF-4 files only) |
'int16' | NC_SHORT |
'uint16' | NC_USHORT (netCDF-4 files only) |
'int8' | NC_BYTE |
'uint8' | NC_UBYTE (netCDF-4 files only) |
'char' | NC_CHAR |
'string' | NC_STRING (netCDF-4 files
only) |
Example: 'Datatype','uint16'
Format
— netCDF file format
'netcdf4_classic'
(default) | character vector | string scalar
NetCDF file format, specified as the comma-separated pair consisting
of 'Format'
and one of these values.
Value of Format | Description |
---|---|
'classic' | netCDF-3 |
'64bit' | netCDF-3, with 64-bit offsets |
'netcdf4_classic' | netCDF-4 classic model |
'netcdf4' | netCDF-4 model (Use this format to enable group hierarchy) |
If varname
specifies a group (for example,'/grid3/temperature'
),
then nccreate
sets the value of Format
to 'netcdf4'
.
Example: 'Format','classic'
FillValue
— Replacement value for missing values
scalar | 'disable'
Replacement value for missing values, specified as the comma-separated pair consisting of
'FillValue'
and a scalar or
'disable'
. The default value is specified by the
netCDF library. To disable replacement values, specify
'FillValue','disable'
. You cannot disable
'FillValue'
when writing data of type
NC_STRING
to netCDF-4 files.
This argument is available for netcdf4
or netcdf4_classic
formats
only.
Example: 'FillValue',NaN
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| char
ChunkSize
— Chunk size along each dimension
vector
Chunk size along each dimension, specified as the comma-separated
pair consisting of 'ChunkSize'
and a vector. The
first element specifies the number of rows, the second element specifies
the number of columns, the third element specifies the length of the
third dimension, and so on. The default value is specified by the
netCDF library.
This argument is available for netcdf4
or netcdf4_classic
formats
only.
Example: 'ChunkSize',[5 6 9]
Data Types: double
DeflateLevel
— Amount of compression
0
(default) | scalar value between 0
and 9
Amount of compression, specified as the comma-separated pair
consisting of 'DeflateLevel'
and a scalar value
between 0
and 9
. 0
indicates
no compression and 9
indicates the most compression.
This argument is available for netcdf4
or netcdf4_classic
formats
only.
Example: 'DeflateLevel',5
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
Shuffle
— Status of shuffle filter
false
(default) | true
Status of the shuffle filter, specified as the comma-separated
pair consisting of 'Shuffle'
and false
or true
. false
disables
the shuffle filter and true
enables it. The shuffle
filter can assist with the compression of integer data by changing
the byte order in the data stream.
This argument is available for netcdf4
or netcdf4_classic
formats
only.
Example: 'Shuffle',true
Data Types: logical
Limitations
You cannot disable
'FillValue'
when writing data of typeNC_STRING
to netCDF-4 files.
Version History
Introduced in R2011aR2021b: Define NC_STRING
Data
You can define NC_STRING
data in netCDF-4 files.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
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)