Using MATLAB Engine API for Python
Mostra commenti meno recenti
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?
Risposte (1)
Kojiro Saito
il 28 Mar 2022
It's because engine.start_matlab launches a new MATALB session and there's no variable in workspace.
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 Commenti
Ankita Tondwalkar
il 28 Mar 2022
Kojiro Saito
il 28 Mar 2022
You can use eval function for accesing the element of MATLAB object.
[Python side]
import matlab.engine
eng = matlab.engine.connect_matlab()
states = eng.eval('env.Model.States')
print(states)
result:
['[1,1]', '[2,1]', '[3,1]', '[4,1]', '[5,1]', '[1,2]', '[2,2]', '[3,2]', '[4,2]', '[5,2]', '[1,3]', '[2,3]', '[3,3]', '[4,3]', '[5,3]', '[1,4]', '[2,4]', '[3,4]', '[4,4]', '[5,4]', '[1,5]', '[2,5]', '[3,5]', '[4,5]', '[5,5]']
[MATLAB side]
% Make MATLAB to be sharable
matlab.engine.shareEngine;
% rlMDPEnv shipping demo
GW = createGridWorld(5,5);
GW.CurrentState = '[2,1]';
GW.TerminalStates = '[5,5]';
GW.ObstacleStates = ["[3,3]";"[3,4]";"[3,5]";"[4,3]"];
updateStateTranstionForObstacles(GW)
GW.T(state2idx(GW,"[2,4]"),:,:) = 0;
GW.T(state2idx(GW,"[2,4]"),state2idx(GW,"[4,4]"),:) = 1;
nS = numel(GW.States);
nA = numel(GW.Actions);
GW.R = -1*ones(nS,nS,nA);
GW.R(state2idx(GW,"[2,4]"),state2idx(GW,"[4,4]"),:) = 5;
GW.R(:,state2idx(GW,GW.TerminalStates),:) = 10;
env = rlMDPEnv(GW);
Ankita Tondwalkar
il 28 Mar 2022
Ankita Tondwalkar
il 3 Apr 2022
Modificato: Ankita Tondwalkar
il 3 Apr 2022
Kojiro Saito
il 4 Apr 2022
I think you need to import GW from MATLAB workspace to Python first.
import matlab.engine
eng = matlab.engine.connect_matlab()
#env = eng.rlMDPEnv("GW")
gw = eng.workspace['GW']
env = eng.rlMDPEnv(gw)
Ankita Tondwalkar
il 4 Apr 2022
Modificato: Ankita Tondwalkar
il 4 Apr 2022
Ankita Tondwalkar
il 7 Apr 2022
Modificato: Ankita Tondwalkar
il 8 Apr 2022
Ankita Tondwalkar
il 8 Apr 2022
Ankita Tondwalkar
il 9 Apr 2022
Modificato: Ankita Tondwalkar
il 9 Apr 2022
Ankita Tondwalkar
il 4 Mag 2022
Ankita Tondwalkar
il 26 Set 2022
Categorie
Scopri di più su Call MATLAB from Python in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!