matlab.engine.EngineError: Unable to connect to MATLAB session 'MATLAB_441'.
19 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentar
Antworten (1)
Infinite_king
am 27 Mai 2024
Bearbeitet: Infinite_king
am 29 Mai 2024
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:
- Close all running sessions of MATLAB.
- 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 -
Connect Python to Running MATLAB Session - https://www.mathworks.com/help/matlab/matlab_external/connect-python-to-running-matlab-session.html
Starting MATLAB Engine - https://www.mathworks.com/help/matlab/apiref/matlab.engine.start_matlab.html
Hope this is helpful.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Call MATLAB from Python finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!