netcdf.defVlen - Define user-defined variable length array type (NC_VLEN) - MATLAB (original) (raw)
Main Content
Define user-defined variable length array type (NC_VLEN)
Since R2022a
Syntax
Description
[typeID](#mw%5Fae6eed05-b905-4ebd-91ad-76729044b15f) = netcdf.defVlen([ncid](#mw%5Fdb3a0b95-afba-448c-940d-c19fdbd2a760%5Fsep%5Fmw%5Ff7e816d5-6ef7-4555-b9bd-92e4e220788a),[typeName](#mw%5F8a7cfaed-71dd-44ca-aa47-d0bf60854d36),[baseType](#mw%5F05cf106b-f5dd-436e-819d-509e60137601))
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
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.
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: [1×1 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
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. ThebaseType
value represents the type of the elements inside the user-defined NC_VLEN
type.
Data Types: string
| char
| double
Output Arguments
Data type identifier for a user-defined NC_VLEN
type, returned as an integer.
Version History
Introduced in R2022a