This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.


Connect shared MATLAB session to MATLAB Engine for Python


eng = matlab.engine.connect_matlab(name=None)
eng = matlab.engine.connect_matlab(___,async)



eng = matlab.engine.connect_matlab(name=None) connects to the shared MATLAB® session, name, and returns a MatlabEngine object as eng. The input argument name specifies the name of a MATLAB session that is already running on your local machine.

  • If you specify name and the engine cannot find a shared MATLAB session of the same name, then you receive an EngineError exception.

  • If you do not specify name and the engine cannot find any shared MATLAB sessions, then it starts a new shared MATLAB session.

  • If you do not specify name and the engine finds multiple shared MATLAB sessions running, then it connects to the first created session.

eng = matlab.engine.connect_matlab(___,async) connects asynchronously if async is True.


collapse all

Connect to a shared MATLAB session that is already running on your local machine.

import matlab.engine
eng = matlab.engine.connect_matlab()

matlab.engine.connect_matlab connects to the first created shared MATLAB session. If no MATLAB sessions are shared, then matlab.engine.connect_matlab starts a new session.

When there are multiple shared MATLAB sessions on your local machine, connect to two different sessions one at a time by specifying their names.

Connect to the first created MATLAB session.

import matlab.engine
names = matlab.engine.find_matlab()
('MATLAB_6830', 'MATLAB_7090')

Connect to the next MATLAB session.

eng = matlab.engine.connect_matlab('MATLAB_7090')

Input Arguments

collapse all

Name of the shared MATLAB session, specified as a character array.

Start MATLAB synchronously or asynchronously, specified as a logical keyword argument.

Example: matlab.engine.start_matlab(async=True)

Output Arguments

collapse all

Python variable for communicating with MATLAB, returned as a MatlabEngine object. eng communicates with a shared MATLAB session that is already running on your local machine


  • Do not connect the engine multiple times to the same shared MATLAB session.

Introduced in R2015b