MATLAB: Matlab.eng​ine.start_​matlab() takes long time to execute even if MATLAB session already exists

delaylongMATLABmatlab.engine.start_matlab

Following MATLAB instructions on how to integrate MATLAB with python, we’re using the following syntax
*import *matlab.engine
future = matlab.engine.start_matlab(async=*True*)
self.eng = future.result()
 
However, either in async or in sync modes , even if MATLAB has been already started and we’re trying to connect to an existing session, it takes about 40 sec
for matlab.engine.start_matlab function to execute.
 

Best Answer

_ matlab.engine.start_matlab() _function starts a MATLAB session. So, if it is taking too long, it is possible that MATLAB itself takes few seconds to start.
matlab.engine.start_matlab() always starts a new MATLAB process, and returns Python variable eng, which is a MatlabEngine object for communicating with the MATLAB process.
You can quickly test the same on a Windows enviroment.
When you call _matlab.engine.start_matlab(), _it will start a new MATLAB process always.
If no argument is passed, it will open a background process.
To confirm that this functions starts a new session always (even though MATLAB session is currently running), please start using "-desktop" option-
eng = matlab.engine.start_matlab("-desktop")
You will see that a new MATLAB Session has been started.
If no option is used, you can see MATLAB in background process.
Run the following command in MATLAB command prompt
>> which -all startup.m
It will show you if you are using any startup file.
Sometime startup file can cause delay.
If you see any startup.m file, try to rename it and try connecting MATLAB engine from Python.