Pass Enumerations to MATLAB from C++
This example shows how to call MATLAB® functions that require MATLAB enumeration members as inputs by defining the enumeration member as a matlab::data::EnumArray
. It uses matlab::data::ArrayFactory
to create the
enumeration array. The matlab::data::EnumArray
contains the
MATLAB class name and one or more enumeration members. You can also pass the
array as a variable to the MATLAB workspace using MATLABEngine::setVariable
.
Note
To pass a matlab::data::EnumArray
to MATLAB, the named MATLAB class must exist and be on the MATLAB path.
Suppose that you define the following TextString
in MATLAB. This class defines a property that is typed as a specific enumeration
class named TextColor
. The TextString
class
constructor takes two input arguments:
Str
— A 1-by-n char arrayColor
— An enumeration member of theTextColor
class.
classdef TextString properties Str(1,:) char Color TextColor end methods function obj = TextString(str,color) if nargin == 2 obj.Str = str; obj.Color = color; end end end end
Here is how to define the MATLAB
TextColor
enumeration class.
classdef TextColor enumeration Red Green Blue end end
This MATLAB statement creates a TextString
object by passing a
character vector and an enumeration member to the class constructor.
T = TextString('Any text string',TextColor.Blue);
The following sample code creates a MATLAB
TextString
object and displays the property values. To create the
TextString
object:
Define a
matlab::data::CharArray
for the MATLAB character vector argument.Define a
matlab::data::EnumArray
for the MATLABTextColor.Blue
enumeration argument.Pass the argument vector to
MATLABEngine::feval
.Get the property values using
MATLABEngine::getProperty
and display the values.
Note
This example requires you to define the MATLAB
TextString
and TextColor
classes described
here. These classes must be on the path of the shared MATLAB session used by this example.
#include "MatlabDataArray.hpp"
#include "MatlabEngine.hpp"
#include <iostream>
void enumArray() {
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;
// Create enumeration array
auto enumColor = factory.createEnumArray({ 1,1 }, "TextColor", { "Blue" });
// Create argument vector
std::vector<matlab::data::Array> args({
factory.createCharArray("Any text string"),
enumColor});
// Call MATLAB TextString to create object
matlab::data::Array T = matlabPtr->feval(u"TextString", args);
// Get the value of the Str property
matlab::data::CharArray c = matlabPtr->getProperty(T, u"Str");
std::cout << "Str property value: " << c.toAscii() << std::endl;
// Get the value of the Color property from EnumArray col
matlab::data::EnumArray col = matlabPtr->getProperty(T, u"Color");
std::cout << "Color property class: " << col.getClassName() << std::endl;
std::cout << "Color property value: " << std::string(col[0]) << std::endl;
}
Here is the program output.
Str property value: Any text string Color property class: TextColor Color property value: Blue
For information on how to setup and build C++ engine programs, see Requirements to Build C++ Engine Applications.
See Also
matlab::data::EnumArray
| matlab::data::ArrayFactory
| matlab::engine::connectMATLAB
| matlab::engine::MATLABEngine