Main Content

netcdf.defVlen

Define user-defined variable length array type (NC_VLEN)

Description

example

typeID = netcdf.defVlen(ncid,typeName,baseType) defines a new NC_VLEN type with the specified type name and base type in the file identified by ncid. The netcdf.defVlen function corresponds to the nc_def_vlen function in the NetCDF library C API.

Examples

collapse all

Define a new NC_VLEN type in a new NetCDF-4 file. Then, create and write a variable of this type. An NC_VLEN type corresponds to a cell array in MATLAB®.

Create a NetCDF-4 file named myfile.nc.

source = "myfile.nc";
cmode = "NETCDF4";
ncid = netcdf.create(source,cmode);

Define a new NC_VLEN type with the MY_VARIABLE_LENGTH_SAMPLE type name and NC_FLOAT base type.

typeName = "MY_VARIABLE_LENGTH_SAMPLE";
baseType = "NC_FLOAT";
typeid = netcdf.defVlen(ncid,typeName,baseType);

Create a new TIME dimension with the length 3 in the NetCDF file.

dimname = "TIME";
dimlen = 3;
dimid = netcdf.defDim(ncid,dimname,dimlen);

Create a new variable named samples of the NC_VLEN type and with the TIME dimension.

varname = "samples";
varid = netcdf.defVar(ncid,varname,typeid,dimid);

Write the data to the NetCDF variable. The data is a cell array that contains arrays of single values. The NC_FLOAT NetCDF data type corresponds to the single data type in MATLAB.

data = {single([0.1,0.2]),single([2.333,7.94,0.5,0]),single(4.2)};
netcdf.putVar(ncid,varid,data)

Close the NetCDF file.

netcdf.close(ncid)

Display the contents of the NetCDF-4 file. The ncdisp function lists the new NC_VLEN type in the User-Defined Types section and represents it by its base data type (single) inside curly braces. The Datatype of the new samples variable shows the name of the NC_VLEN type, MY_VARIABLE_LENGTH_SAMPLE.

ncdisp(source)
Source:
           /tmp/Bdoc22b_2054784_1785327/tp7b1a0acc/matlab-ex80861297/myfile.nc
Format:
           netcdf4
User-Defined Types:
           MY_VARIABLE_LENGTH_SAMPLE: {single}
Dimensions:
           TIME = 3
Variables:
    samples
           Size:       3x1
           Dimensions: TIME
           Datatype:   MY_VARIABLE_LENGTH_SAMPLE

Input Arguments

collapse all

File identifier of an open NetCDF file or OPeNDAP NetCDF data source, specified as an integer. You can retrieve this identifier by using the netcdf.open function.

Data Types: double

Type name for a new NC_VLEN type, specified as a string scalar or character vector.

Data Types: string | char

Base type of the new NC_VLEN type, specified as a string scalar, character vector (such as NC_DOUBLE), or as an integer that is the equivalent numeric type identifier. You can use the netcdf.getConstant function to retrieve the numeric type identifier. The baseType value represents the type of the elements inside the user-defined NC_VLEN type.

Data Types: string | char | double

Output Arguments

collapse all

Data type identifier for a user-defined NC_VLEN type, returned as an integer.

Version History

Introduced in R2022a