Documentation

nccreate

Create variable in NetCDF file

Syntax

  • nccreate(filename,varname) example
  • nccreate(filename,varname,Name,Value) example

Description

example

nccreate(filename,varname) creates a scalar double variable named varname in the NetCDF file specified by filename. If filename does not exist, then nccreate creates the file using the netcdf4_classic format.

example

nccreate(filename,varname,Name,Value) creates a variable with additional options specified by one or more name-value pair arguments. For example, to create a nonscalar variable, use the Dimensions name-value pair argument.

Examples

collapse all

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 pair argument to specify the names and lengths of the two dimensions. Use the 'Format' name-value pair argument to specify the file format.

nccreate('myncclassic.nc','peaks',...
          'Dimensions',{'r',200,'c',200},...
          'Format','classic')

Write data to the variable.

ncwrite('myncclassic.nc','peaks',peaks(200))

Display the contents of the NetCDF file.

ncdisp('myncclassic.nc')
Source:
           pwd\myncclassic.nc
Format:
           classic
Dimensions:
           r = 200
           c = 200
Variables:
    peaks
           Size:       200x200
           Dimensions: r,c
           Datatype:   double

Input Arguments

collapse all

filename — File namestring

File name, specified as a string. 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 variablestring

Name of the new variable, specified as a string.

Example: 'myVar'

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

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 variablecell 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 string followed by its numerical length, in this form: {dname1,dlength1,dname2,dlength2, ...}. The dname1 input is the name of the first dimension, 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) | string

MATLAB data type, specified as the comma-separated pair consisting of 'Datatype' and a string. 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 DatatypeNetCDF Variable Type
'double'NC_DOUBLE
'single'NC_FLOAT
'int64'NC_INT64*
'uint64'NC_UINT64*
'int32'NC_INT
'uint32'NC_UINT*
'int16'NC_SHORT
'uint16'NC_USHORT*
'int8'NC_BYTE
'uint8'NC_UBYTE*
'char'NC_CHAR

* These data types are only available when the file is a netcdf4 format file.

Example: 'Datatype','uint16'

'Format' — NetCDF file format'netcdf4_classic' (default) | string

NetCDF file format, specified as the comma-separated pair consisting of 'Format' and one of the following strings.

Value of FormatDescription
'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 valuesscalar | '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'.

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 dimensionvector

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 compression0 (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 filterfalse (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

Introduced in R2011a

Was this topic helpful?