MATLAB Answers

Calling MATLAB class functions from Python with matlab.engine

9 views (last 30 days)
Javier Perez Tobia
Javier Perez Tobia on 5 Aug 2021
Commented: Yongjian Feng on 9 Aug 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?

Answers (2)

Yongjian Feng
Yongjian Feng on 5 Aug 2021
You first need to instantiate a PU_SMPC object in python, right? You are calling the member method simulation as a static method.
  2 Comments
Yongjian Feng
Yongjian Feng on 9 Aug 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);

Sign in to comment.


Sean de Wolski
Sean de Wolski on 9 Aug 2021
Edited: Sean de Wolski on 9 Aug 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)

Tags

Products

Community Treasure Hunt

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

Start Hunting!