I need to control a Simulink control scheme from an external application, written in Python. What I need to do is to step through the simulation and, at each step, retrieve the output and let the Python application determine the new set of inputs. This for a fixed time period. Is there any way at all to do this? I admit I am having a hard time trying to achieve this using a matlab script, let alone Python. Is this doable? If not, is there a way to insert the Python module into the simulink scheme?
Thanks
EDIT: THIS IS HOW I MANAGED TO SOLVE IT
In order to run the simulation step-by-step, I created this block structure with a clock, a relational operator and an Assertion block
where Tmp is the timestamp of each pause
Tmp=get_param(bdroot,'SimulationTime')
The assertion block contains the following instructions:
set_param(bdroot,'SimulationCommand','pause')
This way, the simulation pauses after each step, i.e. (clockTime-Tmp)=timeStep.
Now, I create a Python script that launches the simulation (see accepted answer) and iterates through like this:
#While the simulation is running
while eng.get_param('simpleTest','SimulationStatus')!=('stopped' or 'terminating'):
if eng.get_param('simpleTest','SimulationStatus')=='paused':
#do your evaluations and operations
eng.set_param('simpleTest','SimulationCommand','update',nargout=0) #if you have updated any simulation parameters
eng.set_param('simpleTest','SimulationCommand','continue',nargout=0)
This seems to work fine for me, but if there are better options, please let me know.