MATLAB: Run Simulink iteration by iteration in MATLAB script

simulink

I have a Simulink model and a MATLAB script. I am collecting data from the Simulink model into the MATLAB script, meaning that I need to run the Simulink model iteration by iteration, because I need the data from my Simulink model in my MATLAB script. I have googled, and found one answer saying to write the following in my script:
sim('name_of_model', [tStart tFinal]);
but this only runs the model from 0 to x seconds, and not iteration by iteration. Can anyone please help me?

Best Answer

When you run a Simulink model from the command-line, it always runs the whole thing. There are some pause/continue controls, but that's not the way to go because you don't really have control over how many time steps run before you pause the model.
I would recommend taking the code in your MATLAB script and placing it into a MATLAB Function block. Then, your model can use that code at every time step as it runs, and can seamlessly accept/return data to/from the rest of the model.
- Sebastian