Main Content

Create Structure Arrays from C++

MATLAB® structures contain data that you reference with field names. Each field can contain any type of data. To access data in a structure, MATLAB code uses dot notation of the form structName.fieldName. The class of a MATLAB structure is struct.

In an array of MATLAB structures, each structure must have the same field names.

For information on how to setup and build C++ engine programs, see Requirements to Build C++ Engine Applications.

Create Structure Array and Send to MATLAB

This example creates a structure array and puts it in the MATLAB workspace.

Here is how to create and send the array.

#include "MatlabDataArray.hpp"
#include "MatlabEngine.hpp"
#include <iostream>

void putStructArray() {
    using namespace matlab::engine;

    // Connect to named shared MATLAB session started as:
    // matlab -r "matlab.engine.shareEngine('myMatlabEngine')"
    String session(u"myMatlabEngine");
    std::unique_ptr<MATLABEngine> matlabPtr = connectMATLAB(session);

    // Create MATLAB data array factory
    matlab::data::ArrayFactory factory;

    // Define 2-element struct array with two fields per struct
    matlab::data::StructArray structArray = factory.createStructArray({ 1, 2}, { "f1", "f2" });

    // Assign values to each field in first struct
    structArray[0]["f1"] = factory.createCharArray("First Data Set");
    structArray[0]["f2"] = factory.createArray<uint8_t>({ 1, 3 }, { 1, 2, 3 });

    // Assign values to each field in second struct
    structArray[1]["f1"] = factory.createCharArray("Second Data Set");
    structArray[1]["f2"] = factory.createArray<double>({ 1, 5 }, { 4., 5., 6., 7., 8. });
    
    // Put struct array in MATLAB workspace
    matlabPtr->setVariable(u"structArray", structArray);
}

Get Structure from MATLAB

This examples shows how to get a structure variable from the MATLAB workspace using the matlab::engine::MATLABEngine getVariable member function.

Note

This example gets a structure array from the MATLAB workspace. This code assumes that there is a structure array variable named structArray in the MATLAB workspace, like the one created in the previous example. To pass the structure array to MATLAB, see Create Structure Array and Send to MATLAB.

#include "MatlabDataArray.hpp"
#include "MatlabEngine.hpp"

void readStructArray() {
    using namespace matlab::engine;

    // Connect to named shared MATLAB session started as:
    // matlab -r "matlab.engine.shareEngine('myMatlabEngine')"
    String session(u"myMatlabEngine");
    std::unique_ptr<MATLABEngine> matlabPtr = connectMATLAB(session);

    // Get the struct array from MATLAB
    matlab::data::StructArray matlabStruct = matlabPtr->getVariable(u"structArray");
}

Access Struct Array Data

There are different ways to access the structure in C++:

  • Create a reference to a particular field. Changes to the reference modify the value in the structure.

  • Create a copy of the field values. Changes to the copy do not modify the values in the structure unless you reassign the value to the structure field.

To get information about the structure array, use the matlab::data::StructArray member functions getDimensions, getNumberOfFields, and getFieldNames

This example follows these steps:

  • Gets the structure array variable named structArray from the MATLAB session.

  • Creates a reference to one of the structure fields.

  • Modifies an element of the double array contained in the field using the reference.

  • Returns the modified structure array to the shared MATLAB session.

This sample code gets the structure array from the shared MATLAB session that was created in a previous section, Create Structure Array and Send to MATLAB.

#include "MatlabDataArray.hpp"
#include "MatlabEngine.hpp"
#include <iostream>

void modifyStructArray() {
    using namespace matlab::engine;

    // Connect to named shared MATLAB session started as:
    // matlab -r "matlab.engine.shareEngine('myMatlabEngine')"
    String session(u"myMatlabEngine");
    std::unique_ptr<MATLABEngine> matlabPtr = connectMATLAB(session);

    // Create MATLAB data array factory
    matlab::data::ArrayFactory factory;

    // Get the struct array from MATLAB
    matlab::data::StructArray matlabStruct = matlabPtr->getVariable(u"structArray");
    matlab::data::ArrayDimensions dims = matlabStruct.getDimensions();
    std::cout << "structArray size is: " << dims[0] << " by " << dims[1] << std::endl;

    // Get number of fields
    size_t numFields = matlabStruct.getNumberOfFields();
    std::cout << "structArray has " << numFields << " fields" << std::endl;
    
    // Get the struct array fieldnames
    Range<ForwardIterator, MATLABFieldIdentifier const> fields = matlabStruct.getFieldNames();
    std::vector<matlab::data::MATLABFieldIdentifier> fieldNames;
    for (const auto& name : fields) {
        fieldNames.push_back(name);
    }

    // Change value of array element using a reference
    matlab::data::TypedArrayRef<double> field1 = matlabStruct[1][fieldNames[1]];
    field1[0] = -200.;

    // Return modified struct array to MATLAB
    matlabPtr->setVariable(u"structArray", matlabStruct);
}

See Also

| | |

Related Topics