matlab::engine::MATLABEngine
R2026bEvaluate MATLAB functions from C++ program
Description
The matlab::engine::MATLABEngine class uses a MATLAB® process as a computational engine for C++. This class provides an interface
between the C++ language and MATLAB, enabling you to evaluate MATLAB functions and expressions from C++ programs.
Class Details
|
Namespace: | matlab::engine |
| Include: | MatlabEngine.hpp |
Member Functions
feval
Evaluate MATLAB functions with input arguments synchronously. Use feval
to pass arguments from C++ to MATLAB and to return a result from MATLAB to C++.
Inputs and outputs can be types defined by the MATLAB Data API or can be native C++ types.
Execute a function and return multiple results.
std::vector<matlab::data::Array> feval(const matlab::engine::String &function,
const size_t numReturned,
const std::vector<matlab::data::Array> &args,
const std::shared_ptr<matlab::engine::StreamBuffer> &output = std::shared_ptr<matlab::engine::StreamBuffer>(),
const std::shared_ptr<matlab::engine::StreamBuffer> &error = std::shared_ptr<matlab::engine::StreamBuffer>())Execute a function with multiple inputs and return a single result.
matlab::data::Array feval(const matlab::engine::String &function,
const std::vector<matlab::data::Array> &args,
const std::shared_ptr<matlab::engine::StreamBuffer> &output = std::shared_ptr<matlab::engine::StreamBuffer>(),
const std::shared_ptr<matlab::engine::StreamBuffer> &error = std::shared_ptr<matlab::engine::StreamBuffer>())Execute a function with a single input and return a single result.
matlab::data::Array feval(const matlab::engine::String &function,
const matlab::data::Array &arg,
const std::shared_ptr<matlab::engine::StreamBuffer> &output = std::shared_ptr<matlab::engine::StreamBuffer>(),
const std::shared_ptr<matlab::engine::StreamBuffer> &error = std::shared_ptr<matlab::engine::StreamBuffer>())Execute a function with native C++ types and redirect output streams.
ResultType feval(const matlab::engine::String &function,
const std::shared_ptr<matlab::engine::StreamBuffer> &output,
const std::shared_ptr<matlab::engine::StreamBuffer> &error,
RhsArgs&&... rhsArgs)Execute a function with native C++ types.
ResultType feval(const matlab::engine::String &function,
RhsArgs&&... rhsArgs)Execute a function with custom C++ structure input and return a custom C++ structure. (since R2026b)
ResultType* outStructPtr = matlabPtr->feval<ResultType*>(const matlab::engine::String &function,inStructPtr)For more details, see Pass C++ Structures to MATLAB Functions.
| Name of the MATLAB function or script to evaluate. Specify the name as an
|
| Number of returned values |
| Multiple input arguments to pass to the MATLAB function in an |
| Single input argument to pass to the MATLAB function |
| Stream buffer used to store the standard output from the MATLAB function. For an example, see Pass Stream Buffer Type for Standard Output or Error. |
| Stream buffer used to store the error message from the MATLAB function. For an example, see Pass Stream Buffer Type for Standard Output or Error. |
| Native C++ data types:
|
(since R2024a) | |
(since R2024a) | |
(since R2026a) | |
| Pointers
to custom C++ structures used as function inputs
( |
| Multiple outputs returned from the MATLAB function |
| Single output returned from the MATLAB function. |
| Output returned from MATLAB function as a user-specified type.
|
| The MATLAB session is not available. |
| There is a MATLAB run-time error in the function. |
| The result of a MATLAB function cannot be converted to the specified type. |
| There is a syntax error in the MATLAB function. |
This example passes an array of numeric values to a MATLAB function. The code performs these steps:
Creates
matlab::data::Arraywith the dimensions 2-by-3 from a vector of numeric values of typedouble.Start a shared MATLAB session.
Pass the data array to the MATLAB
sqrtfunction and return the result to C++.
#include "MatlabDataArray.hpp"
#include "MatlabEngine.hpp"
using namespace matlab::engine;
std::vector<double> cppData{ 4, 8, 12, 16, 20, 24 };
// Create a 2-by-3 MATLAB data array
matlab::data::ArrayFactory factory;
auto inputArray = factory.createArray({ 2, 3 }, cppData.cbegin(), cppData.cend());
// Start MATLAB engine
std::unique_ptr<MATLABEngine> matlabPtr = startMATLAB();
// Pass data array to MATLAB sqrt function
// And return results.
auto result = matlabPtr->feval(u"sqrt", inputArray);When calling feval using native C++ types, the input arguments
are restricted to scalar values. For example, this code returns the square root of a
scalar value.
#include "MatlabEngine.hpp"
using namespace matlab::engine;
// Start MATLAB engine synchronously
std::unique_ptr<MATLABEngine> matlabPtr = startMATLAB();
// Call sqrt function
double result = matlabPtr->feval<double>(u"sqrt", double(27));For functions that return multiple output arguments, you can use the MATLAB data API or, if using C++ types, an std::tuple. For
an example, see Call Function with Native C++ Types.
You can pass custom C++ structure pointers
to feval after associating the structure type with a MATLAB interface to a C++ library using the
USING_TYPE_WITH_MATLAB macro. For example, this code passes two
Position structure pointers to a MATLAB function and receives a Rectangle structure pointer
as output.
#include "MatlabEngine.hpp"
#include "shapes.hpp"
USING_TYPE_WITH_MATLAB(Shapes::Position, "ShapesLib");
USING_TYPE_WITH_MATLAB(Shapes::Rectangle, "ShapesLib");
using namespace matlab::engine;
std::unique_ptr<MATLABEngine> matlabPtr = startMATLAB(MATLABApplicationMode::IN_PROCESS);
Shapes::Position p1{0, 0}, p2{10, 5};
Shapes::Rectangle* rectPtr = matlabPtr->feval<Shapes::Rectangle*>(u"createRectangle", &p1, &p2);
...
delete rectPtr;fevalAsync
Evaluate MATLAB functions with input arguments and returned values asynchronously.
Execute a function asynchronously and return multiple results.
FutureResult<std::vector<matlab::data::Array>> fevalAsync(const matlab::engine::String &function,
const size_t numReturned,
const std::vector<matlab::data::Array> &args,
const std::shared_ptr<matlab::engine::StreamBuffer> &output = std::shared_ptr<matlab::engine::StreamBuffer>(),
const std::shared_ptr<matlab::engine::StreamBuffer> &error = std::shared_ptr<matlab::engine::StreamBuffer>())Execute a function asynchronously with multiple inputs and return a single result.
FutureResult<matlab::data::Array> fevalAsync(const matlab::engine::String &function,
const std::vector<matlab::data::Array> &args,
const std::shared_ptr<matlab::engine::StreamBuffer> &output = std::shared_ptr<matlab::engine::StreamBuffer>(),
const std::shared_ptr<matlab::engine::StreamBuffer> &error = std::shared_ptr<matlab::engine::StreamBuffer>())Execute a function asynchronously with a single input and return a single result.
FutureResult<matlab::data::Array> fevalAsync(const matlab::engine::String &function,
const matlab::data::Array &arg,
const std::shared_ptr<matlab::engine::StreamBuffer> & output = std::shared_ptr<matlab::engine::StreamBuffer>(),
const std::shared_ptr<matlab::engine::StreamBuffer> & error = std::shared_ptr<matlab::engine::StreamBuffer>())Execute a function asynchronously with native C++ types and redirect output streams.
FutureResult<ResultType> fevalAsync(const matlab::engine::String &function,
const std::shared_ptr<matlab::engine::StreamBuffer> &output,
const std::shared_ptr<matlab::engine::StreamBuffer> &error,
RhsArgs&&... rhsArgs)Execute a function asynchronously with native C++ types.
FutureResult<ResultType> fevalAsync(const matlab::engine::String &function,
RhsArgs&&... rhsArgs)Execute a function with custom C++ structure input and return a custom C++ structure. (since R2026b)
FutureResult<ResultType*> future = matlabPtr->fevalAsync<ResultType*>(const matlab::engine::String &function,inStructPtr)For more details, see Pass C++ Structures to MATLAB Functions.
| Name of the MATLAB function or script to evaluate. Specify the name as an
|
| Number of returned values |
| Multiple input arguments to pass to the MATLAB function in an |
| Single input argument to pass to the MATLAB function |
| Stream buffer used to store the standard output from the MATLAB function. For an example, see Pass Stream Buffer Type for Standard Output or Error. |
| Stream buffer used to store the error message from the MATLAB function. For an example, see Pass Stream Buffer Type for Standard Output or Error. |
| Native C++ data types:
|
(since R2024a) | |
(since R2024a) | |
| |
(since R2026a) | |
| Pointers to custom C++
structures used as function inputs ( |
| A |
None
This example passes the scalar double 12.7 to the MATLAB
sqrt function asynchronously. The
FutureResult object is then used to get the result.
#include "MatlabDataArray.hpp"
#include "MatlabEngine.hpp"
using namespace matlab::engine;
std::unique_ptr<MATLABEngine> matlabPtr = startMATLAB();
matlab::data::ArrayFactory factory;
matlab::data::Array argument = factory.createScalar<double>(12.7);
FutureResult<matlab::data::Array> future = matlabPtr->fevalAsync(u"sqrt", std::move(argument));
...
matlab::data::TypedArray<double> result = future.get();You can pass custom C++ structure pointers
to fevalAsync after associating the structure type with a
MATLAB interface to a C++ library using the
USING_TYPE_WITH_MATLAB macro. For example, this code passes two
Position structure pointers to a MATLAB function asynchronously and receives a Rectangle
structure pointer as
output.
#include "MatlabEngine.hpp"
#include "shapes.hpp"
USING_TYPE_WITH_MATLAB(Shapes::Position, "ShapesLib");
USING_TYPE_WITH_MATLAB(Shapes::Rectangle, "ShapesLib");
using namespace matlab::engine;
std::unique_ptr<MATLABEngine> matlabPtr = startMATLAB(MATLABApplicationMode::IN_PROCESS);
Shapes::Position p1{0, 0}, p2{10, 5};
FutureResult<Shapes::Rectangle*> future = matlabPtr->fevalAsync<Shapes::Rectangle*>(u"createRectangle", &p1, &p2);
...
Shapes::Rectangle* rectPtr = future.get();
...
delete rectPtr;eval
Evaluate a MATLAB statement as a string synchronously.
void eval(const matlab::engine::String &statement,
const std::shared_ptr<matlab::engine::StreamBuffer> &output = std::shared_ptr<matlab::engine::StreamBuffer> (),
const std::shared_ptr<matlab::engine::StreamBuffer> &error = std::shared_ptr<matlab::engine::StreamBuffer> ())
| MATLAB statement to evaluate |
| Stream buffer used to store the standard output from the MATLAB statement. For an example, see Redirect Screen Output. |
| Stream buffer used to store the error message from the MATLAB command. For an example, see Redirect Error Output. |
| The MATLAB session is not available. |
| There is a run-time error in the MATLAB statement. |
| There is a syntax error in the MATLAB statement. |
This example evaluates the following MATLAB statement.
a = sqrt(12.7);
The statement creates the variable a in the MATLAB base workspace.
#include "MatlabEngine.hpp"
using namespace matlab::engine;
std::unique_ptr<MATLABEngine> matlabPtr = startMATLAB();
matlabPtr->eval(u"a = sqrt(12.7);");evalAsync
Evaluate a MATLAB statement as a string asynchronously.
FutureResult<void> evalAsync(const matlab::engine::String &str,
const std::shared_ptr<matlab::engine::StreamBuffer> &output = std::shared_ptr<matlab::engine::StreamBuffer> (),
const std::shared_ptr<matlab::engine::StreamBuffer> &error = std::shared_ptr<matlab::engine::StreamBuffer> ())
| MATLAB statement to evaluate |
| Stream buffer used to store the standard output from the MATLAB statement. For an example, see Redirect Screen Output. |
| Stream buffer used to store the error message from the MATLAB command. For an example, see Redirect Error Output. |
| A |
None
This example evaluates the following MATLAB statement asynchronously.
a = sqrt(12.7);
The statement creates the variable a in the MATLAB base workspace.
#include "MatlabEngine.hpp"
using namespace matlab::engine;
std::unique_ptr<MATLABEngine> matlabPtr = startMATLAB();
FutureResult<void> future = matlabPtr->evalAsync(u"a = sqrt(12.7);");getVariable
Get a variable from the MATLAB base or global workspace.
matlab::data::Array getVariable(const matlab::engine::String &varName,
matlab::engine::WorkspaceType workspaceType = matlab::engine::WorkspaceType::BASE)
| Name of a variable in the MATLAB workspace. Specify the name as an
|
| MATLAB workspace (BASE or GLOBAL) to get the variable from. For
more information, see |
| Variable obtained from the MATLAB base or global workspace |
| The MATLAB session is not available. |
| The requested variable does not exist in the specified MATLAB base or global workspace. |
This example gets a variable named varName from the MATLAB base workspace.
#include "MatlabEngine.hpp"
using namespace matlab::engine;
std::unique_ptr<MATLABEngine> matlabPtr = startMATLAB();
matlab::data::Array varName = matlabPtr->getVariable(u"varName");getVariableAsync
Get a variable from the MATLAB base or global workspace asynchronously.
FutureResult<matlab::data::Array> getVariableAsync(const matlab::engine::String &varName,
matlab::engine::WorkspaceType workspaceType = matlab::engine::WorkspaceType::BASE)
| Name of the variable in MATLAB workspace. Specify the name as an
|
| MATLAB workspace (BASE or GLOBAL) to get the variable from. For
more information, see |
| A |
None
This example gets a variable named varName from the MATLAB base workspace asynchronously.
#include "MatlabEngine.hpp"
using namespace matlab::engine;
std::unique_ptr<MATLABEngine> matlabPtr = startMATLAB();
FutureResult<matlab::data::Array> future = matlabPtr->getVariableAsync(u"varName");
...
matlab::data::Array varName = future.get();setVariable
Put a variable into the MATLAB base or global workspace.
If a variable with the same name exists in the MATLAB workspace, setVariable overwrites it.
void setVariable(const matlab::engine::String &varName,
const matlab::data::Array &var,
matlab::engine::WorkspaceType workspaceType = matlab::engine::WorkspaceType::BASE)
| Name of the variable to create in the MATLAB workspace. Specify the name as an
|
| Value of the variable to create in the MATLAB workspace |
| Put the variable in the MATLAB BASE or GLOBAL workspace. For more information, see
|
| The MATLAB session is not available. |
This example puts the variable named data in the MATLAB base workspace.
#include "MatlabEngine.hpp"
using namespace matlab::engine;
std::unique_ptr<MATLABEngine> matlabPtr = startMATLAB();
matlab::data::Array data = factory.createArray<double>({ 1, 3 }, { 4, 8, 6 });
matlabPtr->setVariable(u"data", data);setVariableAsync
Put a variable into the MATLAB base or global workspace asynchronously.
If a variable with the same name exists in the MATLAB base workspace, setVariableAsync overwrites it.
FutureResult<void> setVariableAsync(const matlab::engine::String &varName,
const matlab::data::Array var,
matlab::engine::WorkspaceType workspaceType = matlab::engine::WorkspaceType::BASE)
| Name of the variable to create in the MATLAB workspace. Specify the name as an
|
| Value of the variable to create in the MATLAB workspace |
| Put the variable in the MATLAB BASE or GLOBAL workspace. For more information, see
|
None
This example puts the variable named data in the MATLAB base workspace.
#include "MatlabEngine.hpp"
using namespace matlab::engine;
std::unique_ptr<MATLABEngine> matlabPtr = startMATLAB();
matlab::data::Array data = factory.createArray<double>({ 1, 3 }, { 4., 8., 6. });
FutureResult<void> future = matlabPtr->setVariableAsync(u"data", data);getProperty
Get the value of an object property.
If the object input argument is an array of objects, specify the index of the array element that corresponds to the object whose property value you want to get.
Get a property value from an element of an object array
matlab::data::Array getProperty(const matlab::data::Array &objectArray,
size_t index,
const matlab::engine::String &propertyName)Get a property value from a scalar object
matlab::data::Array getProperty(const matlab::data::Array &object,
const matlab::engine::String &propertyName)
| Array of MATLAB objects |
| Scalar MATLAB object |
| Zero-based index into the object array, specifying the object in that array whose property value is returned |
| Name of the property. Specify the name as an
|
| Value of the named property |
| The MATLAB session is not available. |
| The property does not exist. |
This example evaluates a MATLAB statement in a try/catch block using
MATLABEngine::eval. The
MATLABEngine::getVariable member function returns the exception
object. MATLABEngine::getProperty returns the exception
message property value as a
matlab::data::CharArray.
#include "MatlabEngine.hpp"
using namespace matlab::engine;
std::unique_ptr<MATLABEngine> matlabPtr = startMATLAB();
matlabPtr->eval(u"try; surf(4); catch me; end");
matlab::data::Array mException = matlabPtr->getVariable(u"me");
matlab::data::CharArray message = matlabPtr->getProperty(mException, u"message");
std::cout << "messages is: " << message.toAscii() << std::endl;getPropertyAsync
Get the value of an object property asynchronously.
If the object input argument is an array of objects, specify the index of the array element that corresponds to the object whose property value you want to get.
Get a property value asynchronously from an element of an object array
FutureResult<matlab::data::Array> getPropertyAsync(const matlab::data::Array &objectArray,
size_t index,
const matlab::engine::String &propertyName)Get a property value asynchronously from a scalar object
FutureResult<matlab::data::Array> getPropertyAsync(const matlab::data::Array &object,
const matlab::engine::String &propertyName)
| Array of MATLAB objects |
| Scalar MATLAB object |
| Zero-based index into the object array, specifying the object in that array whose property value is returned |
| Name of the property. Specify the name as an
|
|
|
None
This example evaluates a MATLAB statement in a try/catch block using
MATLABEngine::eval. The
MATLABEngine::getVariable member function returns the exception
object. MATLABEngine::getPropertyAsync returns a
FutureResult that you use to get the exception
message property value as a
matlab::data::CharArray.
#include "MatlabEngine.hpp"
using namespace matlab::engine;
std::unique_ptr<MATLABEngine> matlabPtr = startMATLAB();
matlabPtr->eval(u"try;surf(4);catch me;end");
matlab::data::Array mException = matlabPtr->getVariable(u"me");
FutureResult<matlab::data::Array> future = matlabPtr->getPropertyAsync(mException, u"message");
matlab::data::CharArray message = future.get();
std::cout << "messages is: " << message.toAscii() << std::endl;setProperty
Set the value of an object property.
If the object input argument is an array of objects, specify the index of the array element that corresponds to the object whose property value you want to set.
Set a property value on an element of an object array
void setProperty(matlab::data::Array &objectArray,
size_t index,
const matlab::engine::String &propertyName,
const matlab::data::Array &propertyValue)Set a property value on a scalar object
void setProperty(matlab::data::Array &object,
const matlab::engine::String &propertyName,
const matlab::data::Array &propertyValue)
| Array of MATLAB objects |
| Scalar MATLAB object |
| Zero-based index into the object array, specifying the object in that array whose property value is set |
| Name of the property to set. Specify the name as an
|
const matlab::data::Array
&propertyValue | Value assigned to the property |
| The MATLAB session is not available. |
| The property does not exist. |
This example shows how to set a MATLAB object property. It creates a MATLAB graph and returns the line handle object. Setting the value of the line
LineStyle property to the character :
changes the property value of the line object in MATLAB and updates the line style of the graph.
#include "MatlabEngine.hpp"
using namespace matlab::engine;
std::unique_ptr<MATLABEngine> matlabPtr = startMATLAB();
matlab::data::ArrayFactory factory;
matlab::data::Array yData = factory.createArray<double>({ 1, 5 }, { 4.0, 11.0, 4.7, 36.2, 72.3 });
matlab::data::Array lineHandle = matlabPtr->feval(u"plot", yData);
matlab::data::CharArray lineStyle = factory.createCharArray(":");
matlabPtr->setProperty(lineHandle, u"LineStyle", lineStyle);setPropertyAsync
Set the value of an object property asynchronously.
If the object input argument is an array of objects, specify the index of the array element that corresponds to the object whose property value you want to set.
Set a property value asynchronously on an element of an object array
FutureResult<void> setPropertyAsync(matlab::data::Array &objectArray,
size_t index,
const matlab::engine::String &propertyName,
const matlab::data::Array &propertyValue)Set a property value asynchronously on a scalar object
FutureResult<void> setPropertyAsync(matlab::data::Array &object,
const matlab::engine::String &propertyName,
const matlab::data::Array &propertyValue)
| Array of MATLAB objects |
| Scalar MATLAB object |
| Zero-based index into the object array, specifying the object in that array whose property value is set |
| Name of the property to set. Specify the name as an
|
const matlab::data::Array
&propertyValue | Value assigned to the property. |
None
This example shows how to set a MATLAB object property asynchronously. It creates a MATLAB graph and returns the line handle object. Setting the line
LineStyle property to the character :
changes the property value of the object in MATLAB and updates the line style of the graph.
#include "MatlabEngine.hpp"
using namespace matlab::engine;
std::unique_ptr<MATLABEngine> matlabPtr = startMATLAB();
matlab::data::ArrayFactory factory;
matlab::data::Array yData = factory.createArray<double>({ 1, 5 }, { 4.0, 11.0, 4.7, 36.2, 72.3 });
matlab::data::Array lineHandle = matlabPtr->feval(u"plot", yData);
matlab::data::CharArray lineStyle = factory.createCharArray(":");
FutureResult<void> future = matlabPtr->setPropertyAsync(lineHandle, u"LineStyle", lineStyle);