Main Content

Pass Variables from MATLAB to C++

Pass variables from the MATLAB® base or global workspace to your C++ program using the matlab::engine::MATLABEngine getVariable and getVariableAsync member functions. Return the variable to C++ as a matlab::data::Array.

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

Bring Result of MATLAB Calculation Into C++

This sample code performs a calculation in MATLAB using MATLABEngine::eval and gets the results using MATLABEngine::getVariable.

The MATLAB cart2sph function converts a point in Cartesian coordinates to its representation in spherical coordinates.

#include "MatlabDataArray.hpp"
#include "MatlabEngine.hpp"
#include <iostream>
void callgetVars() {
    using namespace matlab::engine;

    // Start MATLAB engine synchronously
    std::unique_ptr<MATLABEngine> matlabPtr = startMATLAB();

    // Evaluate MATLAB statement
    matlabPtr->eval(u"[az,el,r] = cart2sph(5,7,3);");

   // Get the result from MATLAB
    matlab::data::TypedArray<double> result1 = matlabPtr->getVariable(u"az");
    matlab::data::TypedArray<double> result2 = matlabPtr->getVariable(u"el");
    matlab::data::TypedArray<double> result3 = matlabPtr->getVariable(u"r");

    // Display results
    std::cout << "az: " << result1[0] << std::endl;
    std::cout << "el: " << result2[0] << std::endl;
    std::cout << "r: " << result3[0] << std::endl;
}

Get MATLAB Objects and Access Properties

Use the matlab::engine::MATLABEngine getVariable or getVariableAsync member functions to get MATLAB object variables. Return the object to C++ as a matlab::data::Array. Access object properties using the matlab::engine::MATLABEngine getProperty or getPropertyAsync member functions.

This sample code creates a MATLAB figure object and returns the object handle to C++. Values of the figure Units property are always character arrays. Therefore, to query the figure Units property, return a matlab::data::CharArray with the value of the figure property (default value is pixels).

#include "MatlabDataArray.hpp"
#include "MatlabEngine.hpp"
#include <iostream>
void callgetVariables() {
    using namespace matlab::engine;

    // Start MATLAB engine synchronously
    std::unique_ptr<MATLABEngine> matlabPtr = startMATLAB();

    // Create figure window
    matlabPtr->eval(u"figureHandle = figure;");

    //Get figure handle and Units property
    matlab::data::Array figHandle = matlabPtr->getVariable(u"figureHandle");
    matlab::data::CharArray units = matlabPtr->getProperty(figHandle, u"Units");

    // Display property value
    std::cout << "Units property: " << units.toAscii() << std::endl;
}

Get Property from Object Array

If the object variable is an array of objects, call getProperty or getPropertyAsync with the index of the object in the array that you want to access. For example, to get the value of the Units property of the fourth element in the object array, objectArray, specify the index as the second input argument.

matlab::data::CharArray units = matlabPtr->getProperty(objectArray, 3, u"Units");

Set Property on MATLAB Object

To set the value of a MATLAB object property from C++, use the matlab::engine::MATLABEngine setProperty or setPropertyAsync member function. To access the object property, you can get the object variable from the MATLAB workspace and set the value on the C++ variable. The property value that you set updates the object in the MATLAB workspace.

You can also return an object from a call to MATLABEngine::feval and MATLABEngine::fevalAsync and set property values on that object.

This sample code creates a MATLAB figure object and returns the object to C++. The code sets the figure Color property to red, which changes the value of the property on the object in the MATLAB workspace.

Because the value of the Color property is a char array, use the matlab::data::ArrayFactory to create a matlab::data::CharArray to define the new value.

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

    // Start MATLAB engine synchronously
    std::unique_ptr<MATLABEngine> matlabPtr = startMATLAB();

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

    // Create figure window
    size_t numArguments(1);
    std::vector<matlab::data::Array> figureHandle = matlabPtr->feval(u"figure", numArguments, {});

    // Pause to display the figure
    matlabPtr->eval(u"pause(5)");

    // Set the Color property to red
    matlabPtr->setProperty(figureHandle[0], u"Color", factory.createCharArray("red"));
    matlabPtr->eval(u"pause(10)");
}

Set Property from Object Array

If the object variable is an array of objects, call setProperty or setPropertyAsync with the index of the object in the array that you want to access. For example, to set the value of the Color property of the fourth element in the object array, objectArray, specify the index as the second input argument.

matlabPtr->setProperty(objectArray, 3, u"Color", factory.createCharArray("red"));

See Also

|

Related Topics