Calling matlab.eng​ine.connec​t_matlab() twice freezes Python

3 visualizaciones (últimos 30 días)
Rob Campbell
Rob Campbell el 28 de En. de 2023
Comentada: Rob Campbell el 30 de En. de 2023
I can connect to a named running MATLAB session from Python with:
In [8]: matlab.engine.connect_matlab('MY_SESSION')
But if I call it a second time Python hangs for about a minute before finally saying it can not connect
In [8]: matlab.engine.connect_matlab('MY_SESSION')
Out[8]: <matlab.engine.matlabengine.MatlabEngine at 0x1648f46a370>
In [9]: matlab.engine.connect_matlab('MY_SESSION')
...
EngineError: Unable to connect to MATLAB session 'MY_SESSION'.
I can not find a way of testing if MY_SESSION already exists. I also can not find a way to close a connection that already exists in a different scope. Are those things possible?

Respuestas (1)

Chris
Chris el 29 de En. de 2023
Editada: Chris el 29 de En. de 2023
names = matlab.engine.find_matlab()
if 'MY_SESSION' in names:
eng = matlab.engine.connect_matlab('MY_SESSION')
You can del(eng) to close the connection in the present scope, but I'm not sure about forcing it closed from somewhere else.
Since Matlab takes a bit to do its thing, connecting/reconnecting will sometimes throw an error. You might use a try/except or a while loop to handle the error in a python script/program.
  5 comentarios
Chris
Chris el 30 de En. de 2023
Editada: Chris el 30 de En. de 2023
Then if the variable is present, the connection is there. Make a function that tests that condition before connecting.
Rob Campbell
Rob Campbell el 30 de En. de 2023
We are getting side tracked now.
I want a way to test whether the connection has been made and it looks like that does not exist.
Thanks for trying, though.

Iniciar sesión para comentar.

Categorías

Más información sobre Call MATLAB from Python en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by