Calling MATLAB class functions from Python with matlab.engine

23 vues (au cours des 30 derniers jours)
Javier Perez Tobia
Javier Perez Tobia le 5 Août 2021
I have a MATLAB MPC (PU_SMPC) class that I can properly initialize within Python with the matlab.engine API by running:
import matlab.engine
eng = matlab.engine.start_matlab()
smpc = eng.PU_SMPC(params, nargout=1)
which returns a matlab.object. However, I cannot figure out how to run the PU_SMPC method simulation, which takes an integer as an input. Here's a summary of what I have tried and the errors I get:
smpc.simulation(60)
Traceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: 'matlab.object' object has no attribute 'simulation'
,
eng.simulation(smpc, 60)
Error using PU_SMPC/simulation
Too many output arguments.
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Users\Usuario\PycharmProjects\Adaptive_SMPC\matlab\engine\matlabengine.py", line 70, in __call__
return FutureResult(self._engine(), future, nargs, _stdout,
File "C:\Users\Usuario\PycharmProjects\Adaptive_SMPC\matlab\engine\futureresult.py", line 67, in result
return self.__future.result(timeout)
File "C:\Users\Usuario\PycharmProjects\Adaptive_SMPC\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: Too many output arguments.
,
eng.simulation(60)
Undefined function 'simulation' for input arguments of type 'int64'.
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Users\Usuario\PycharmProjects\Adaptive_SMPC\matlab\engine\matlabengine.py", line 70, in __call__
return FutureResult(self._engine(), future, nargs, _stdout,
File "C:\Users\Usuario\PycharmProjects\Adaptive_SMPC\matlab\engine\futureresult.py", line 67, in result
return self.__future.result(timeout)
File "C:\Users\Usuario\PycharmProjects\Adaptive_SMPC\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: Undefined function 'simulation' for input arguments of type 'int64'.
end
,
eng.PU_SMPC.simulation(60)
Unrecognized function or variable 'PU_SMPC.simulation'.
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Users\Usuario\PycharmProjects\Adaptive_SMPC\matlab\engine\matlabengine.py", line 70, in __call__
return FutureResult(self._engine(), future, nargs, _stdout,
File "C:\Users\Usuario\PycharmProjects\Adaptive_SMPC\matlab\engine\futureresult.py", line 67, in result
return self.__future.result(timeout)
File "C:\Users\Usuario\PycharmProjects\Adaptive_SMPC\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: Undefined function 'PU_SMPC.simulation' for input arguments of type 'int64'.
end
,
eng.PU_SMPC.simulation(smpc, 60)
Unrecognized function or variable 'PU_SMPC.simulation'.
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Users\Usuario\PycharmProjects\Adaptive_SMPC\matlab\engine\matlabengine.py", line 70, in __call__
return FutureResult(self._engine(), future, nargs, _stdout,
File "C:\Users\Usuario\PycharmProjects\Adaptive_SMPC\matlab\engine\futureresult.py", line 67, in result
return self.__future.result(timeout)
File "C:\Users\Usuario\PycharmProjects\Adaptive_SMPC\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: Undefined function 'PU_SMPC.simulation' for input arguments of type 'PU_SMPC'.
end
I cannot access any of the class member from Python.
Here's a sample of what the class looks like:
classdef PU_SMPC < handle
properties
A;
B;
C;
end
methods
function obj = PU_SMPC(A,B,C)
A = A;
B = B;
C = C;
end
function simulation(obj, simulation_time)
%code to run simulation
end
end
Any suggestions?

Réponses (2)

Sean de Wolski
Sean de Wolski le 9 Août 2021
Modifié(e) : Sean de Wolski le 9 Août 2021
MWE works for me. Specify nargout, and make sure your directories are on the path (strongly recommend using Projects for this, i.e. openProject() being the first thing you do in python engine so that the entire MATLAB is setup for what you're working on)

Yongjian Feng
Yongjian Feng le 5 Août 2021
You first need to instantiate a PU_SMPC object in python, right? You are calling the member method simulation as a static method.
  3 commentaires
Yongjian Feng
Yongjian Feng le 9 Août 2021
I see. It seems like the second way is close to success. The only thing missing is that pythonengine expects simulation to return something.
Can you modify your simulation method to return an integer. And then repeate second way:
eng.simulation(smpc, 60);
Indhu Priyadharshini Govindasamy
I am trying to call matlab workspace variable in python using matlab engine API
method one
eng.eval('a = simout;',nargout=0)
eng.eval('b = tout;',nargout=0)
mpi = eng.workspace['a']
ki=eng.workspace['b']
method two
mpi = eng.workspace['simout']
ki=eng.workspace['tout']
i used these lines of code
i can get simout values in python but for tout i am getting this error
matlab.engine.MatlabExecutionError: Unrecognized function or variable 'tout'.
could you please help me to fix this?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Call MATLAB from Python dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by