Main Content

netcdf.defVlen

Define user-defined variable length array type (NC_VLEN)

Since R2022a

Description

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.

example

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)

Show the datatype and dimensions of the samples variable using the ncinfo function. The Variables field of the info structure shows the datatype that you defined and the Dimensions subfield shows the dimension that you defined.

info = ncinfo(source);
info.Variables
ans = struct with fields:
            Name: 'samples'
      Dimensions: [1x1 struct]
            Size: 3
        Datatype: 'MY_VARIABLE_LENGTH_SAMPLE'
      Attributes: []
       ChunkSize: []
       FillValue: {[]}
    DeflateLevel: []
         Shuffle: 0

info.Variables.Dimensions
ans = struct with fields:
         Name: 'TIME'
       Length: 3
    Unlimited: 0

Input Arguments

collapse all

Identifier of a netCDF source, specified as a nonnegative integer scalar. The netCDF source can be a netCDF file or a netCDF group.

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

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