필터 지우기
필터 지우기

Using MATLAB Engine API for Python

조회 수: 20 (최근 30일)
Ankita Tondwalkar
Ankita Tondwalkar 2022년 3월 27일
댓글: Ankita Tondwalkar 2022년 9월 26일
I have a variable A= 25 (1X1 double) in my MATLAB workspace and I am using the MATLAB Engine API to call it in python by using the following code.
import matlab.engine
eng = matlab.engine.start_matlab()
var = eng.workspace['A']
print(var)
I am getting the following error message:
Error using matlab.internal.engine.getVariable
Undefined variable 'A'.
Traceback (most recent call last):
File "readingdata.py", line 20, in <module>
test = eng.workspace['A']
File "/usr/local/lib/python3.8/dist-packages/matlab/engine/matlabengine.py", line 120, in __getitem__
future = _method(attr)
File "/usr/local/lib/python3.8/dist-packages/matlab/engine/matlabengine.py", line 70, in __call__
return FutureResult(self._engine(), future, nargs, _stdout,
File "/usr/local/lib/python3.8/dist-packages/matlab/engine/futureresult.py", line 67, in result
return self.__future.result(timeout)
File "/usr/local/lib/python3.8/dist-packages/matlab/engine/fevalfuture.py", line 82, in result
self._result = pythonengine.getFEvalResult(self._future,self._nargout, None, out=self._out, err=self._err)
matlab.engine.MatlabExecutionError:
File /usr/local/MATLAB/R2022a/toolbox/matlab/external/engines/engine_api/+matlab/+internal/+engine/getVariable.m, line 27, in getVariable
Undefined variable 'A'.
I could do the same using scipy.io
Is there something I am missing?

답변 (1개)

Kojiro Saito
Kojiro Saito 2022년 3월 28일
It's because engine.start_matlab launches a new MATALB session and there's no variable in workspace.
You need to create a session to a running MATLAB as described in this document.
In MATLAB, you need to call matlab.engine.shareEngine.
[MATLAB side]
A = 25;
matlab.engine.shareEngine
Then, you need to create a session from Python using engine.connect_matlab not engine.start_matlab.
[Python side]
import matlab.engine
eng = matlab.engine.connect_matlab()
var = eng.workspace['A']
print(var)
  댓글 수: 11
Ankita Tondwalkar
Ankita Tondwalkar 2022년 5월 4일
This problem resulted from that fact how indexing works differenty in MATLAB and Python. I made sure while callig in Python I followed the indexing rule for Python.
Ankita Tondwalkar
Ankita Tondwalkar 2022년 9월 26일
Hello,
I am trying to access predefined enviromen ("BasicGridWorld") variable, obsInfo and actInfo present in MATLAB workspace in Python.
While doing so, I am able to find my MATLAB session but unable to connect to it.
In my Python terminal I am getting the following error:
matlab.engine.EngineError: Unable to connect to MATLAB session 'MATLAB_3052'.
In MATLAB I am using the share engine command (but I get the error that "The current MATLAB session is shared already")while in Python I have the matlab.engine.connect_matlab(). Can anyone let me know if I am missing something?
Thanks,
Ankita

댓글을 달려면 로그인하십시오.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by