You can connect the C++ engine to shared MATLAB® sessions that are running on the local machine. To connect to a shared MATLAB session:
Start MATLAB as a shared session, or make a running MATLAB process shared using the matlab.engine.shareEngine
MATLAB function.
Find the names of the MATLAB shared sessions using matlab::engine::findMATLAB
or matlab::engine::findMATLABAsync
.
Pass a matlab::engine::String
containing the name of the shared MATLAB session to the matlab::engine::connectMATLAB
or matlab::engine::connectMATLABAsync
member function. These functions connect the C++ engine to the shared session.
If you do not specify the name of a shared MATLAB session when calling matlab::engine::connectMATLAB
or matlab::engine::connectMATLABAsync
, the engine uses the first shared MATLAB session created. If there are no shared MATLAB sessions available, the engine creates a shared MATLAB session and connects to this session.
For information on how to setup and build C++ engine programs, see Build C++ Engine Programs.
This sample code connects to the first shared MATLAB session found.
#include "MatlabEngine.hpp"
void syncConnect() { using namespace matlab::engine; // Connect to shared MATLAB session std::unique_ptr<MATLABEngine> matlabPtr = connectMATLAB(); }
This sample code connects to the first shared MATLAB session found asynchronously.
#include "MatlabEngine.hpp"
void asyncConnect() { using namespace matlab::engine; // Find and connect to shared MATLAB session FutureResult<std::unique_ptr<MATLABEngine>> futureMATLAB = connectMATLABAsync(); ... std::unique_ptr<MATLABEngine> matlabPtr = futureMATLAB.get(); }
You can specify the name of the shared MATLAB session when you execute the matlab.engine.shareEngine
MATLAB function. Doing so eliminates the need to use matlab::engine::findMATLAB
or matlab::engine::findMATLABAsync
to find the name.
For example, start MATLAB and name the shared session myMatlabEngine
.
matlab -r "matlab.engine.shareEngine('myMatlabEngine')"
This sample code connects to the MATLAB session named myMatlabEngine
from C++.
Start the named MATLAB session before connecting from the C++ code.
#include "MatlabEngine.hpp"
void connectToML() { using namespace matlab::engine; // Connect to named shared MATLAB session started as: // matlab -r "matlab.engine.shareEngine('myMatlabEngine')" std::unique_ptr<MATLABEngine> matlabPtr = connectMATLAB(u"myMatlabEngine"); }
To connect to a named MATLAB shared session, use matlab::engine::findMATLAB
or matlab::engine::findMATLABAsync
to find the names of all available named MATLAB shared sessions.
This sample code tries to find a MATLAB shared session named myMatlabEngine
and connects to it if the session is found.
void findNConnect() { using namespace matlab::engine; // Find and connect to shared MATLAB session std::unique_ptr<MATLABEngine> matlabPtr; std::vector<String> names = findMATLAB(); std::vector<String>::iterator it; it = std::find(names.begin(), names.end(), u"myMatlabEngine"); if (it != names.end()) { matlabPtr = connectMATLAB(*it); } // Determine if engine connected if (matlabPtr){ matlab::data::ArrayFactory factory; matlab::data::CharArray arg = factory.createCharArray("-release"); matlab::data::CharArray version = matlabPtr->feval(u"version", arg); std::cout << "Connected to: " << version.toAscii() << std::endl; } else { std::cout << "myMatlabEngine not found" << std::endl; } }
matlab.engine.shareEngine
| matlab::engine::connectMATLAB
| matlab::engine::connectMATLABAsync
| matlab::engine::findMATLAB
| matlab::engine::findMATLABAsync