Hi everyone,
I would like to use a controller written in python for a simulink enviornment and therefore I am using the python matlab API. But the problem is how do i get information (state information for each time step) from the simulink enviornment to my python script. I have tried the following in python:
import matlab.engineimport matplotlib.pyplot as plteng = matlab.engine.start_matlab()modelName = 'plant'eng.eval("model = '{}'".format(modelName), nargout=0)eng.eval("load_system(model)", nargout=0)# Initialize Control Action to 0u = 0eng.set_param('{}/u'.format(modelName), 'value', str(u), nargout=0)print("Initialized Model")# Start Simulation and then Instantly pauseeng.set_param(modelName, 'SimulationCommand', 'start', 'SimulationCommand', 'pause', nargout=0)print(eng.who())eng.set_param(modelName, 'SimulationCommand', 'stop', nargout=0)eng.quit()
The simulink model "plant" is attached where I use "To workspace" block to load the data in to the workspace. But I don't know how to get the data now of the object "eng.workspace['out']"
MATLAB version: R2020b
Python: 3.7.9
Thanks in advance
Best Answer