Create MATLAB Array of C++ Objects
To create a MATLAB® object that represents C++ native arrays or std::vector
types, call the MATLAB
clibArray
function.
In MATLAB, you refer to this class as clib.libname.MyClass.
Create an array of five MyClass objects.
The type of the MATLAB array myclassArray is
clib.array.libname.MyClass. To access an element of
myclassArray, use MATLAB indexing. For example, access the first element.
e = myclassArray(1)
The element type is clib.libname.MyClass.
Alternatively, if the element type is a fundamental type, a user-defined class with a
default constructor, or a standard string type, call the clib.array
constructor.
To create an array from a fundamental type, you must know the corresponding element
type. For mapping information, see the Vector Integer Types and Floating Point Types tables in
C++ to MATLAB Data Type Mapping. For example,
if the C++ type is std::vector<int32_t>, then the MATLAB element
type is clib.libname.Int. Create an array with five
clib.libname.Int elements.
myIntArray = clib.array.libname.Int(5)
Create an array of five elements of the same user-defined class.
myclassArray = clib.array.libname.MyClass(5)
To create an array from a standard string type, see the std::vector<T> String Types table for
element type information. For example, if the C++ type is
std::vector<std::string>, then the MATLAB element type is
clib.libname.std.String. Create an array with five
clib.libname.std.String elements.
myStringArray = clib.array.libname.std.String(5)
Convert MATLAB Array to C++ Array Object
You can use an existing MATLAB array as a C++ array object. For example, suppose that you have a
MATLAB interface libname. Convert a MATLAB array of double values to a C++ array object by
using the clibConvertArray function.
a = [1 2 3];
arr = clibConvertArray("clib.libname.Double",a);
class(arr)
'clib.array.libname.Double'
Treat C++ Native Arrays of Fundamental Types as MATLAB Fundamental Types
By default, MATLAB represents C++ native arrays of fundamental types with the MATLAB
clib.array types. If you need to preserve fundamental MATLAB array types with outputs, then build your MATLAB interface to the C++ library with the ReturnCArrays
name-value argument set to false. For more information, see
clibgen.generateLibraryDefinition.
Note
Saving C++ objects into a MAT-file is not supported.
Note
You cannot create an array of C++ objects using square brackets.
See Also
clibArray | clibConvertArray | clibgen.generateLibraryDefinition