matlab.eng​ine.Engine​Error: Unable to connect to MATLAB session 'MATLAB_441'.

조회 수: 18 (최근 30일)
Hud Bin
Hud Bin 2024년 4월 18일
편집: Infinite_king 2024년 5월 29일
The situation I have is similar to this. Essentially, I followed this model to have a single container with matlab and python. My code uses start_matlab():
```
class MatlabEngine():
def __init__(self, paths=[]):
self.paths = paths
self.eng = self.startup()
def startup(self):
self.eng = matlab.engine.start_matlab()
for p in self.paths:
self.eng.addpath(p, nargout=0)
return self.eng
```
giving this error: matlab.engine.EngineError: Unable to launch Simple server: Unable to launch /opt/matlab/R2023a/bin/matlab
So I opened a docker and ran `matlab.engine.shareEngine`, and then in a separate terminal, I connected to the same container and was able to run connect_matlab in python directly.
```
>>> import matlab.engine
>>> eng = matlab.engine.connect_matlab()
>>> eng.sqrt(4.0)
2.0
```
However, when I applied the same to my code I get:
```
Traceback (most recent call last):
File "/home/matlab/t-prime/TPrime_transformer/TPrime_transformer_train.py", line 241, in <module>
ds_test = TPrimeDataset_Transformer(protocols=protocols, ds_type='test', file_postfix=args.postfix, ds_path=exp_config["raw_path"], snr_dbs=args.snr_db, seq_len=exp_config["Sequence length"], slice_len=exp_config["Slice length"], slice_overlap_ratio=0, raw_data_ratio=args.dataset_ratio,
File "/home/matlab/t-prime/TPrime_transformer/../preprocessing/TPrime_dataset.py", line 396, in __init__
super().__init__(**kwargs)
File "/home/matlab/t-prime/TPrime_transformer/../preprocessing/TPrime_dataset.py", line 122, in __init__
self.mateng = matutils.MatlabEngine() # todo check if we need any custom paths
File "/home/matlab/t-prime/TPrime_transformer/../preprocessing/matutils/matutils.py", line 8, in __init__
self.eng = self.startup()
File "/home/matlab/t-prime/TPrime_transformer/../preprocessing/matutils/matutils.py", line 17, in startup
self.eng = matlab.engine.connect_matlab()
File "/home/matlab/t-prime/TPrime_transformer/../matlab/engine/__init__.py", line 188, in connect_matlab
eng = future.result()
File "/home/matlab/t-prime/TPrime_transformer/../matlab/engine/futureresult.py", line 67, in result
return self.__future.result(timeout)
File "/home/matlab/t-prime/TPrime_transformer/../matlab/engine/matlabfuture.py", line 87, in result
handle = pythonengine.getMATLAB(self._future)
matlab.engine.EngineError: Unable to connect to MATLAB session 'MATLAB_441'.
```

답변 (1개)

Infinite_king
Infinite_king 2024년 5월 27일
편집: Infinite_king 2024년 5월 29일
Hi Hud Bin,
Based on your description, it seems you are attempting to connect to the MATLAB engine from Python using 'matlab.engine.start_matlab()', but this approach is encountering errors. The connection works successfully if the MATLAB session is started manually and then converted into a shared session.
Try the following steps and see if it works,
  • Close all running sessions of MATLAB.
  • Open a new MATLAB session and enter 'matlab.engine.shareEngine' to convert it into a shared session.
  • Alternatively, you can use the command "matlab -r "matlab.engine.shareEngine" from the terminal to start MATLAB in shared mode.
  • Create a sample Python file to connect to the shared session using 'matlab.engine.connect_matlab()'. For example
# sample.py
import matlab.engine
eng = matlab.engine.connect_matlab()
eng.sqrt(4.0)
  • Run the Python file. If it runs successfully, it means the connection to MATLAB has been established.
  • Note - Keep the MATLAB session running in the background.
Additionally, if you still want to use 'matlab.engine.start_matlab()', try the following:
  1. Close all running sessions of MATLAB.
  2. Create a sample Python file to start and connect to the MATLAB session
# sample2.py
import matlab.engine
eng = matlab.engine.start_matlab("-desktop")
eng.sqrt(4.0)
For more information refer the following MATLAB documentations -
Hope this is helpful.

카테고리

Help CenterFile Exchange에서 Call MATLAB from Python에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by