Main Content


Add NetCDF schema definitions to NetCDF file




ncwriteschema(filename,schema) creates or adds attributes, dimensions, variable definitions and group structure defined in schema to the file filename.

Use ncwriteschema in combination with ncinfo to create a new NetCDF file based on the schema of an existing file. You can also use ncwriteschema to add variable definitions, attributes, dimensions, or group structure to an existing file.


ncwriteschema does not write variable data. Use ncwrite to write data to the created variables. Created unlimited dimensions will have an initial size of 0 until you write data.


ncwriteschema cannot change the format of an existing file. It cannot redefine existing variables and dimensions in filename. If your schema contains attributes, dimensions, variable definitions, or a group structure that already exist in the file, writeschema issues a warning but continues processing.

Input Arguments


Character vector or string scalar containing the name of a NetCDF file. If filename does not exist, ncwriteschema creates a new file using the netcdf4_classic format, unless the Format field in schema specifies another format.


A structure, or array of structures, representing either a dimension, variable, an entire NetCDF file, or a netcdf4 group. A group or file schema can contain a dimension or variable schema, or both. You can use the output returned by ncinfo as a schema structure. The following table lists the fields in the various types of schema structures. Optional fields are marked with asterisk (*).

Schema TypeStructure FieldDescription
Group/File SchemaNameCharacter vector or string scalar identifying the group name. Use '/' to indicate the entire file.
 Dimensions*Dimension schema
 Variables*Variable schema
 Attributes*Structure array of group/global attributes with Name and Value fields
 Format*Character vector or string scalar identifying a NetCDF file format
Dimension schemaNameCharacter vector or string scalar identifying the dimension
 LengthLength of the dimension. Can be Inf.
 Unlimited*Boolean flag indicating if the dimension is unlimited
 Format*Character vector or string scalar identifying a NetCDF file format
Variable schemaNameCharacter vector or string scalar identifying a variable name
 DimensionsVariable's dimension schema
 DatatypeCharacter vector or string scalar identifying a MATLAB® datatype
 Attributes*Structure array of variable attributes with Name and Value fields
 ChunkSize*Numeric value specifying chunk size of the variable
 FillValue*Text or numeric fill value
 DeflateValue*Deflate compression level
 Shuffle* Boolean flag to turn on the Shuffle filter
 Format*Character vector or string scalar identifying a NetCDF file format


Create a classic format file with two dimension definitions.

mySchema.Name   = '/';
mySchema.Format = 'classic';
mySchema.Dimensions(1).Name   = 'time';
mySchema.Dimensions(1).Length = Inf;
mySchema.Dimensions(2).Name   = 'rows';
mySchema.Dimensions(2).Length = 10;
ncwriteschema('', mySchema);

Create a netcdf4_classic format file to store a single variable from an existing file. First use ncinfo to get the schema of the peaks variable from the file. Then use ncwriteschema to create a NetCDF file, defining the peaks variable. Use ncread to get the data associated with the peaks variable and then use ncwrite to write the data to the variable in the new NetCDF file.

myVarSchema = ncinfo('','peaks');
peaksData   = ncread('','peaks');

Version History

Introduced in R2011a