C++ Cell Arrays - MATLAB & Simulink (original) (raw)

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({ 1, 4 }, { 2.2, 3.3, -4.2, 6.0}), factory.createArray({ 1, 4 }, { true, true, false, true }), factory.createScalar(-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 doubleArray = C[1][0]; doubleArray[0] = -2.2;

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

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

See Also

matlab::data::ArrayFactory

Topics