Main Content

C++ Cell Arrays

To create a cell array, use the matlab::data::ArrayFactory createCellArray function.

Create a CellArray that is equivalent to a MATLAB® cell array defined with this MATLAB statement. Note that MATLAB assigns the cells in column-major order.

C = {'Character Array',...
    [true true false true];...
    [2.2 3.3 -4.2 6.0],...
    int32(-3374)};

Create an ArrayFactory:

matlab::data::ArrayFactory factory;

Call createCellArray and define each cell contained in the cell array:

matlab::data::CellArray C = factory.createCellArray({ 2,2 },
    factory.createCharArray("Character Array"),
    factory.createArray<double>({ 1, 4 }, { 2.2, 3.3, -4.2, 6.0}),
    factory.createArray<bool>({ 1, 4 }, { true, true, false, true }),
    factory.createScalar<int32_t>(-3374)
    );

Modify the array by overwriting the value in the cell referred to in MATLAB as C{1,1}.

C[0][0] = factory.createCharArray("New Character Array");

Get a reference to the cell containing the double array and change the first element to -2.2.

TypedArrayRef<double> doubleArray = C[1][0];
doubleArray[0] = -2.2;

Display the new values in the cell containing the double array:

TypedArray<double> const newArray =  C[1][0];
for (auto e : newArray) {
    std::cout << e << std::endl;
} 

See Also

Related Topics