MATLAB® provides an interface, clib.array
, which wraps C++ native
arrays and std::vector
types. The term clib array
refers to the MATLAB representation of these C++ types.
A MATLAB clib array is only defined when the corresponding C++ native array or
std::vector
is used by supported C++ constructs—function input and
output arguments and data members. The provided headers must contain the definition of the
element type. The construct must be supported by MATLAB and not dropped when building the interface.
To create a MATLAB object to pass to a C++ function, call the MATLAB
clibArray
function. For example, suppose that your library libname
defines a
class myclass
. In MATLAB, you refer to this class as clib.libname.myclass
. To
create an array of five myclass
objects, use this syntax:
myclassArray = clibArray('clib.libname.myclass',5);
The type of the MATLAB array myclassArray
is
clib.array.libname.myclass
. To access an element of
myclassArray
, use MATLAB indexing. For example, to access the first element, use this
syntax:
e = myclassArray(1)
The type of the element is clib.libname.myclass
.
Note
Saving C++ objects into a MAT-file is not supported.
You can use an existing MATLAB array as a C++ array object. Call the clibConvertArray
function.
MATLAB arrays created with clibArray
or
clibConvertArray
have these properties.
Property | Type | Access | Description |
---|---|---|---|
| double vector | read-only | C++ dimensions of the array |
| logical scalar | read-only |
|
MATLAB arrays created with clibArray
or
clibConvertArray
have these methods.
Method | Signature | Description |
---|---|---|
| append([element]) | Add an optionally specified element to the end of the array. For a primitive MATLAB clib array, if there is no input argument, then a zero value is appended. For a class-type MATLAB clib array, if there is no input argument, then the class-type default constructor is appended. If the class-type default constructor is deleted, a runtime error occurs. |
| removeLast | Remove the last element of the array. If the MATLAB clib array is empty, a runtime error occurs. |
| double | Convert to double precision. |
|
| Convert to |
|
| Convert to |
|
| Convert to |
|
| Convert to |
|
| Convert to |
|
| Convert to |
|
| Convert to |
|
| Convert to |
| logical | Convert numeric values to logical. |
By default, MATLAB represents std::vector
types with the MATLAB
clib.array
type. If you need to preserve fundamental MATLAB array types with outputs, then build your interface with the
ReturnCArrays
argument set to false
. For more
information, see clibgen.generateLibraryDefinition
.
The memory for MATLAB arrays created with clibArray
or
clibConvertArray
is owned by MATLAB. To release the memory, call clibRelease
.
clibArray
| clibConvertArray
| clibRelease