Main Content


C++ class to access MATLAB object arrays


Use ObjectArray objects to access MATLAB® object arrays. To create an ObjectArray, call createArray in the ArrayFactory class using this syntax:

template <typename ItType, typename T>
TypedArray<T> createArray(ArrayDimensions dims, 
    ItType begin, 
    ItType end)

To create a scalar object, call createScalar using this syntax:

ObjectArray createScalar(const Object& val);

ObjectArray is defined as:

using ObjectArray = TypedArray<Object>;

Class Details





You cannot combine elements of an ObjectArray into a heterogeneous array.

If the class defining the Object overrides subsref or subsasgn, then you cannot access the elements of the ObjectArray.


Create ObjectArray

Create an ObjectArray from myObject class objects. The iterators are pointers to the beginning and the end of the array.

class myObject {
    const std::vector<matlab::data::Object>& getObjs() const {
       return fObjs;
    std::vector<matlab::data::Object> fObjs;

const myObject& a1;
const myObject& a2;
matlab::data::ArrayFactory factory;
const auto& objs = a1.getObjs();
matlab::data::ObjectArray arr1 = factory.createArray({1,2}, objs.begin(), objs.end());

Iterate Through ObjectArray

Iterate using a range-based for loop through an ObjectArray and retrieve the objects in the array.

std::vector<matlab::data::Object> fObjs;

// Use a range-based for loop to iterate over the objects.
for (const auto& o : objs) { 

Objects in MEX and Engine Applications

C++ MEX and C++ Engine applications can get and set property values on MATLAB objects. For information on how to access MATLAB objects in these applications, see these topics:

Introduced in R2017b