Main Content

Connect Java to Running MATLAB Session

Find and Connect to MATLAB

You can connect the Java® engine to shared MATLAB® sessions that are running on the local machine. To connect to a shared MATLAB session:

  • Start MATLAB as a shared engine session, or make a running MATLAB process shared using matlab.engine.shareEngine.

  • Find the names of the MATLAB shared sessions using the MatlabEngine.findMatlab or MatlabEngine.findMatlabAsync static method.

  • Pass the string containing the name of the shared MATLAB session to the MatlabEngine.connectMatlab or MatlabEngine.connectMatlabAsync static method. These methods connect the Java engine to the shared session.

If you do not specify the name of a shared MATLAB session when calling MatlabEngine.connectMatlab or MatlabEngine.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 a description of these methods, see com.mathworks.engine.MatlabEngine

Connect to MATLAB Synchronously

Convert the MATLAB session to a shared session by calling matlab.engine.shareEngine from MATLAB.

matlab.engine.shareEngine

Find the session and connect synchronously from Java.

import  com.mathworks.engine.*;

public class javaFindConnect {
    public static void main(String[] args) throws Exception {
        String[] engines = MatlabEngine.findMatlab();
        MatlabEngine eng = MatlabEngine.connectMatlab(engines[0]);
        // Execute command on shared MATLAB session
        eng.eval("plot(1:10); print('myPlot','-djpeg')");
        eng.close();
    }
}

Connect to MATLAB Asynchronously

Convert the MATLAB session to a shared session by calling matlab.engine.shareEngine from MATLAB.

matlab.engine.shareEngine

Find the session and connect asynchronously from Java.

import  com.mathworks.engine.*;
import java.util.concurrent.Future;

public class javaFindConnectAsync {
    public static void main(String[] args) throws Exception {
        Future<String[]> eFuture = MatlabEngine.findMatlabAsync();
        String[] engines = eFuture.get();
        Future<MatlabEngine> engFuture = MatlabEngine.connectMatlabAsync(engines[0]);
        // Work on other thread
        MatlabEngine eng = engFuture.get();
        // Execute command on shared MATLAB session
        Future<Void> vFuture = eng.evalAsync("plot(1:10); print('myPlot','-djpeg')");
        eng.close();
    }
}

Specify Name of Shared Session

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 MatlabEngine.findMatlab or MatlabEngine.findMatlabAsync to find the name.

For example, start MATLAB and name the shared session myMatlabEngine.

matlab -r "matlab.engine.shareEngine('myMatlabEngine')"

Connect to the named MATLAB session from Java.

import  com.mathworks.engine.*;

public class javaNameConnect {
    public static void main(String[] args) throws Exception {
        String[] myEngine = {"myMatlabEngine"};
        MatlabEngine eng = MatlabEngine.connectMatlab(myEngine[0]);
        // Execute command on shared MATLAB session
        eng.eval("plot(1:10); print('myPlot','-djpeg')");
        eng.close();
    }
}

Related Topics