Connect shared MATLAB session to MATLAB Engine for Python
to the shared MATLAB® session,
eng = matlab.engine.connect_matlab(
name, and returns
MatlabEngine object as
The input argument
name specifies the name of a MATLAB session
that is already running on your local machine.
If you specify
nameand the engine cannot find a shared MATLAB session of the same name, then you receive an
If you do not specify
nameand the engine cannot find any shared MATLAB sessions, then it starts a new shared MATLAB session.
If you do not specify
nameand the engine finds multiple shared MATLAB sessions running, then it connects to the first created session.
Connect to MATLAB Session
Connect to a shared MATLAB session that is already running on your local machine.
import matlab.engine eng = matlab.engine.connect_matlab() eng.sqrt(4.0)
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
Connect to MATLAB Sessions by Name
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() names
Connect to the next MATLAB session.
eng = matlab.engine.connect_matlab('MATLAB_7090') eng.sqrt(4.0)
name — Name of shared MATLAB session
Name of the shared MATLAB session, specified as a character array. The name must be a valid MATLAB variable name.
background — Start MATLAB synchronously or asynchronously
False (default) | logical
Connect to MATLAB synchronously or asynchronously, specified as a logical keyword argument.
eng — Python variable for communicating with MATLAB
Python variable for communicating with MATLAB, returned
with a shared MATLAB session that is already running on your
Do not connect the engine multiple times to the same shared MATLAB session.
Version HistoryIntroduced in R2015b
background Argument to Start Engine Asynchronously
As of Python Version 3.7,
async is a keyword and cannot be used as an
matlab.engine.start_matlab. Use the
background argument instead for all supported versions of
Python as listed in Versions of Python Compatible with MATLAB Products by Release.