To call MATLAB® functions that require MATLAB enumeration members as inputs, define the enumeration member as a matlab::data::EnumArray. Use 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 array
Color — An enumeration member of the TextColor 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 MATLAB
TextColor.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
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 Build C++ Engine Programs.
matlab::data::ArrayFactory | matlab::engine::connectMATLAB | matlab::engine::MATLABEngine