Main Content


Create new netCDF dataset


ncid = netcdf.create(filename,cmode)
[chunksize_out,ncid] = netcdf.create(filename,cmode,initsz,chunksize)


ncid = netcdf.create(filename,cmode) creates a new netCDF file according to the file creation mode. The return value ncid is a file ID. The cmode parameter determines the type of file access. Specify cmode as one of these values.

Value of cmodeDescription
'NOCLOBBER'Prevent overwriting of existing file with the same name.
'CLOBBER'Overwrite any existing file with the same name.
'SHARE'Allow synchronous file updates.
'64BIT_OFFSET'Allow easier creation of files and variables which are larger than two gigabytes.
'NETCDF4'Create a netCDF-4/HDF5 file
'CLASSIC_MODEL'Enforce the classic model; has no effect unless used in a bitwise-or with NETCDF4


You can specify the mode as a numeric value, retrieved using the netcdf.getConstant function. To specify more than one mode, use a bitwise-OR of the numeric values of the modes.

[chunksize_out,ncid] = netcdf.create(filename,cmode,initsz,chunksize) creates a new netCDF file, but with additional performance tuning parameters. initsz sets the initial size of the file. chunksize can affect I/O performance. The actual value chosen by the netCDF library might not correspond to the input value.

This function corresponds to the nc_create and nc__create functions in the netCDF library C API. To use this function, you should be familiar with the netCDF programming paradigm.


collapse all

Create a netCDF dataset named, only if no other file with the same name exists in the current directory. To run this example, you must have write permission in your current directory.

ncid = netcdf.create('','NOCLOBBER')
ncid = 65536

netcdf.create returns a file identifier.

Close the file


Get the numeric values corresponding to the NETCDF4 and CLASSIC_MODEL constants defined by the netCDF library. Use a bitwise-OR of the numeric values to specify more than one creation mode.

cmode = netcdf.getConstant('NETCDF4');
cmode = bitor(cmode,netcdf.getConstant('CLASSIC_MODEL'));

Create a netCDF-4 file that uses the classic model by specifying the creation mode value, cmode.

ncid = netcdf.create('',cmode);

Close the file.


Create a netCDF file that includes a variable of type NC_UINT, one of the data types introduced in netCDF-4.

First, create a netCDF file Because the data type NC_UINT is available only in netCDF-4, specify "NETCDF4" as the file creation mode.

ncid = netcdf.create("","NETCDF4");

Create a scalar variable observation of type NC_UINT in

observation_id = netcdf.defVar(ncid,"observation","NC_UINT",[]);

Write a value of 1 into observation.


Read the value of observation to confirm expected behavior.

observation_value = netcdf.getVar(ncid,observation_id)
observation_value = uint32

Close the file.


Version History

Introduced in R2008b