Main Content

ConnectPythonto RunningMATLAB年代ession

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 SharedMATLAB年代ession

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

matlab.engine.shareEngine

年代tart Python at the operating system prompt. To connect to the shared MATLAB session, callmatlab.engine.connect_matlabfrom Python. You can call any MATLAB function from Python.

导入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, callmatlab.engine.find_matlabfrom Python.

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

matlab.engine.find_matlab返回一个tuplewith the names of all shared MATLAB sessions on your local machine. In this examplematlab.engine.shareEnginegave the shared session the default nameMATLAB_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连接到第一次会议在thetuplereturned bymatlab.engine.find_matlab.

Connect Asynchronously to SharedMATLAB年代ession

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

matlab.engine.shareEngine

年代tart Python at the operating system prompt. Connect asynchronously to the shared MATLAB session.

导入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 SharedMATLAB年代essions

You can connect to multiple shared MATLAB sessions from Python.

年代tart a second MATLAB session. From MATLAB callmatlab.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, seeVariable Names.

matlab.engine.shareEngine('MATLABEngine2')

From Python, find all shared MATLAB sessions.

导入matlab。引擎matlab.engine.find_matlab ()
('MATLAB_13232','MATLABEngine2')

To connect to the shared MATLAB sessions, callmatlab.engine.connect_matlabfrom Python.

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

年代tart SharedMATLAB年代essions with Startup Options

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

年代tart 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.

年代ee Also

||||

Related Topics