Main Content

Connect C++ to Running MATLAB Session

These examples show how to connect the C++ engine to shared MATLAB® sessions that are running on the local machine. To connect to a shared MATLAB 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 Requirements to Build C++ Engine Applications.

Connect to Shared MATLAB

This example 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();
}

Connect to Shared MATLAB Asynchronously

This example 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();
}

Specify Name of Shared Session

This example shows how to 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++.

Note

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");
}

Find and Connect to Named Shared Session

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 example 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;
    }
}

Connect in Multiple-Thread Environments

You can make these connections in a multiple-thread environment to shared MATLAB sessions:

  • Connect to different shared MATLAB sessions from separate threads of a C++ application.

  • Connect to a single MATLAB session from multiple engine applications.

You cannot use multiple threads of the same process to connect to a single shared MATLAB session.

See Also

| | | |

Related Topics