Can not connect to Matlab session from Python, I can start matlab engine but can not connect from python

8 visualizzazioni (ultimi 30 giorni)
In python:
import matlab.engine
print(matlab.engine.find_matlab())
('MATLAB_9012',)
eng = matlab.engine.connect_matlab('MATLAB_9012')
eng.sqrt(4.0)
EngineError Traceback (most recent call last) Cell In[132], line 1----> 1 eng = matlab.engine.connect_matlab('MATLAB_9012') 2 eng.sqrt(4.0) File ~\anaconda3\Lib\site-packages\matlab\engine\__init__.py:201, in connect_matlab(name, **kwargs) 199 future = FutureResult(name=name, attach=True) 200 if not background: --> 201 eng = future.result() 202 return eng 203 else: File ~\anaconda3\Lib\site-packages\matlab\engine\futureresult.py:62, in FutureResult.result(self, timeout) 59 if timeout < 0: 60 raise TypeError(pythonengine.getMessage('TimeoutCannotBeNegative')) ---> 62 return self.__future.result(timeout) File ~\anaconda3\Lib\site-packages\matlab\engine\matlabfuture.py:87, in MatlabFuture.result(self, timeout) 84 else: 85 raise TimeoutError(pythonengine.getMessage('LaunchMatlabTimeout')) ---> 87 handle = pythonengine.getMATLAB(self._future) 88 eng = MatlabEngine(handle) 89 self._matlab = eng EngineError: Unable to connect to MATLAB session 'MATLAB_9012'.
-----------------------------------------------------------------------------------
If I do this:
import matlab.engine
# Start the MATLAB engine
eng = matlab.engine.start_matlab()
# Check if the engine is running
print("MATLAB Engine started:", eng)
MATLAB Engine started: <matlab.engine.matlabengine.MatlabEngine object at 0x0000024483C419A0>
I can start Matlab engine but I can not connect to a running Matlab session????

Risposte (1)

Gabor
Gabor il 19 Ott 2024
It took some time to figure this out, so it may help someone else, I am very new to Python.
I wanted to call a variable in python from current running matlab session which is open and print the variable out in python. It looks like the scipt does not work in jupiter notebook file .ipynb ONLY in python console. Here is how I was able to achive this simple task:
  1. I defined a =1; in matlab
  2. check the matlab session name in matlab:
>> disp(['Current MATLAB session PID: ', num2str(feature('getpid'))]);
Current MATLAB session PID: 31704
3. !!! IN PYTHON CONSOLE !!! ( ANACONDA NAVIGATOR -> ANACONDA TOOLBOX -> PLUS SIGN LEFT TOP -> SELECT: CONSOLE (NOT notebook)
import matlab.engine
eng = matlab.engine.connect_matlab('MATLAB_31704') # This pid session code that I have got from matlab session
pid = eng.eval("feature('getpid')")
print(f"Connected to MATLAB session PID: {pid}")
print(f"a_fromMatlab: {eng.workspace["a"]}")
eng.quit()
Anytime i have got error in python I had to quite the session: eng.quit() and restart the connection process. It took me a couple hours to figure this out, and very annoying that something does not work in a code file only in a console.

Prodotti


Release

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by