Main Content

Connect Python to Running MATLAB Session

You can connect the MATLAB® Engine for Python® to a shared MATLAB session that is already running on your local machine. You also can connect to multiple shared MATLAB sessions from a single Python session. You can share a MATLAB session at any time during the session, or at start with a startup option.

Connect to Shared MATLAB Session

First, convert your MATLAB session to a shared session. From MATLAB call matlab.engine.shareEngine.

matlab.engine.shareEngine

Start Python at the operating system prompt. To connect to the shared MATLAB session, call matlab.engine.connect_matlab from Python. You can call any MATLAB function from Python.

import matlab.engine
eng = matlab.engine.connect_matlab()
eng.sqrt(4.0)
2.0

You can connect to a shared session by name. To find the name of a shared session, call matlab.engine.find_matlab from Python.

matlab.engine.find_matlab()
('MATLAB_13232',)

matlab.engine.find_matlab returns a tuple with the names of all shared MATLAB sessions on your local machine. In this example matlab.engine.shareEngine gave the shared session the default name MATLAB_13232, where 13232 is the ID of the MATLAB process. The operating system gives the MATLAB session a different process ID whenever you start MATLAB.

Connect to the MATLAB session by name.

eng.quit()
newEngine = matlab.engine.connect_matlab('MATLAB_13232')

If you do not specify the name of a shared session, matlab.engine.connect_matlab connects to the first session named in the tuple returned by matlab.engine.find_matlab.

Connect Asynchronously to Shared MATLAB Session

From MATLAB, convert your MATLAB session to a shared session.

matlab.engine.shareEngine

Start Python at the operating system prompt. Connect asynchronously to the shared MATLAB session.

import matlab.engine
future = matlab.engine.connect_matlab(background=True)
eng = future.result()

Call a MATLAB function from Python.

eng.sqrt(4.0)
2.0

Connect to Multiple Shared MATLAB Sessions

You can connect to multiple shared MATLAB sessions from Python.

Start a second MATLAB session. From MATLAB call matlab.engine.shareEngine. Give a name to the second shared session. The name must be a valid MATLAB variable name. For information on valid variable names, see Variable Names.

matlab.engine.shareEngine('MATLABEngine2')

From Python, find all shared MATLAB sessions.

import matlab.engine
matlab.engine.find_matlab()
('MATLAB_13232','MATLABEngine2')

To connect to the shared MATLAB sessions, call matlab.engine.connect_matlab from Python.

eng1 = matlab.engine.connect_matlab('MATLAB_13232')
eng2 = matlab.engine.connect_matlab('MATLABEngine2')

Start Shared MATLAB Sessions with Startup Options

By default MATLAB sessions are not shared. However, you can start MATLAB as a shared session with a startup option.

Start shared MATLAB sessions at the operating system prompt.

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

You can start a session with a default name, or give a name enclosed in single quotes.

See Also

| | | |

Related Topics