Main Content

matlab.engine.connect_matlab

Connect sharedMATLABsession toMATLABEngine forPython

Description

example

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

  • If you specifynameand the engine cannot find a shared MATLAB session of the same name, then you receive anEngineErrorexception.

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

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

eng= matlab.engine.connect_matlab(___,background)connects asynchronously ifbackgroundisTrue. You can use this syntax with thenameinput argument in the previous syntax.

eng= matlab.engine.connect_matlab(___,async)connects asynchronously ifasyncisTrue. Not recommended. Use thebackgroundargument instead. Do not use for Python®Version 3.7 or later. For more information, seeCompatibility Considerations.

Examples

全部折叠

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)
2.0

matlab.engine.connect_matlabconnects to the first created shared MATLAB session. If no MATLAB sessions are shared, thenmatlab.engine.connect_matlabstarts 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() names
('MATLAB_6830', 'MATLAB_7090')

Connect to the next MATLAB session.

eng = matlab.engine.connect_matlab('MATLAB_7090') eng.sqrt(4.0)
2.0

Input Arguments

全部折叠

Name of the shared MATLAB session, specified as a character array. The name must be a valid MATLAB variable name.

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

Example:matlab.engine.connect_matlab(背景= True)

Connect to MATLAB synchronously or asynchronously, specified as a logical keyword argument. Not recommended. Use thebackgroundargument instead. For more information, seeCompatibility Considerations.

Output Arguments

全部折叠

Python variable for communicating with MATLAB, returned as aMatlabEngineobject.engcommunicates with a shared MATLAB session that is already running on your local machine

Limitations

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

Version History

Introduced in R2015b

expand all