Main Content

Create Arrays with C++ MATLAB Data API

Create Arrays

The C++ MATLAB® Data API lets applications running outside of MATLAB work with MATLAB data through a MATLAB-neutral interface. The API uses modern C++ semantics and design patterns and avoids data copies whenever possible by using MATLAB copy-on-write semantics.

The header file for the MATLAB Data API is MatlabDataArray.hpp.

The matlab::data::Array class is the base class for all array types. It provides general array information, such as type and size. The Array class supports both one-dimensional and multi-dimensional arrays. The MATLAB Data API uses zero-based indexing.

To create an array, first create a factory using matlab::data::ArrayFactory.

matlab::data::ArrayFactory factory;

Use the factory to create a 2-by-2 array of type double. Specify the array values in column-major format to match the ordering of the MATLAB statement A = [1 2; 3 4]. To inspect the array, use the functions in the matlab::data::Array class.

#include "MatlabDataArray.hpp"

int main() {
    using namespace matlab::data;
    ArrayFactory factory;
    Array A = factory.createArray<double>({ 2,2 },
        { 1.0, 3.0, 2.0, 4.0 });

    // Inspect array
    ArrayType c = A.getType();
    ArrayDimensions d = A.getDimensions();
    size_t n = A.getNumberOfElements();

    return 0;
}

This code is equivalent to the following MATLAB statements.

A = [1 2; 3 4];
c = class(A);
d = size(A);
n = numel(A);

The matlab::data::TypedArray class supports iterators, which enable you to use range-based for loops. This example creates a 1-by-6 array from the 3-by-2 TypedArray.

#include "MatlabDataArray.hpp"

int main() {
    using namespace matlab::data;
    ArrayFactory factory;

    // Create a 3-by-2 TypedArray 
    TypedArray<double>  A = factory.createArray( {3,2},
        {1.1, 2.2, 3.3, 4.4, 5.5, 6.6 }); 

    // Assign values of A to the double array C   
    double C[6];
    int i = 0;
    for (auto e : A) {
        C[i++] = e;
    }

    return 0;
}

Create Column Major Array from Row Major Data

If you have an std::vector created with data in row major order, you can create a column major TypedArray in MATLAB with its data.

#include "MatlabDataArray.hpp"

int main() {
    using namespace matlab::data;
    ArrayFactory factory;
    const std::vector<double> data{1, 2, 3, 4, 5, 6};

    // Create a 2-by-3 TypedArray in column-major order
    TypedArray<double>  A = factory.createArray( {2,3},
        data.begin(), data.end(), ROW_MAJOR); 

    // Assign values of A to the double array C   
    double C[6];
    int i = 0;
    for (auto e : A) {
        C[i++] = e;
    }

    return 0;
}

Operate on Each Element in an Array

Modify each element in a matlab::data::Array using a reference to the element. This example multiplies each element in the matlab::data::TypedArray by a scalar value.

#include "MatlabDataArray.hpp"

int main() {
    using namespace matlab::data;
    ArrayFactory factory;

    // Create a 3-by-2 TypedArray 
    TypedArray<double>  A = factory.createArray( {3,2},
        {1.1, 2.2, 3.3, 4.4, 5.5, 6.6 }); 

    // Define scalar multiplier
    double multiplier(10.2);

    // Multiple each element in A 
    for (auto& elem : A) {
        elem *= multiplier;
    }

    return 0;
}

See Also

|