Main Content

C++ cell 配列

cell 配列の作成には関数 matlab::data::ArrayFactory createCellArray を使用します。

この MATLAB® ステートメントによって定義される MATLAB cell 配列と等価の CellArray を作成します。MATLAB は cell を列優先の順序で代入することに注意してください。

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

ArrayFactory を作成します。

matlab::data::ArrayFactory factory;

createCellArray を呼び出して、cell 配列に含まれる各 cell を定義します。

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)
    );

MATLAB で C{1,1} として参照されている cell の値を上書きすることにより、配列を変更します。

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

double 配列を含む cell への参照を取得し、1 番目の要素を -2.2 に変更します。

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

double 配列を含む cell 内の新しい値を表示します。

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

参考

関連するトピック