MATLAB: How to run a real-time simulation multiple times consecutively from within MATLAB using Real-Time Windows Target

consecutiveexternalseveralsim

I have a Simulink model that I would like to run using Real-Time Windows Target. I want to run this model many times consecutively from a MATLAB file. The problem is that, after I start the simulation once, I need the MATLAB file to wait until the simulation is finished before it moves on. It does not do this automatically, since the simulation is started in external mode with the command:
set_param(gcs,'SimulationCommand','start')
If I use the PAUSE function, both the MATLAB file and the Simulink simulation pause. The functionality I require is for the MATLAB file to pause while the simulation continues to run.

Best Answer

The ability to pause execution of a MATLAB file without pausing an external Simulink simulation is not available in MATLAB.
To work around this issue, you can use either the KEYBOARD function or MATLAB timer objects.
Inserting the KEYBOARD command stops execution of the file and gives control to the keyboard, allowing the user to determine manually when to continue execution. This requires manual user input.
You can automate the required functionality using MATLAB timer functions. The WAIT function in MATLAB pauses execution of a MATLAB file while allowing a Simulink simulation to continue. Here is an example of how this could be done:
for j = 1:50
T = timer('TimerFcn',@init_sim); %initialize timer object
start(T) %start timer object running
end
function init_sim(obj,event) %timer object callback function
set_param(gcs,'SimulationMode','external') %simulate Simulink model
set_param(gcs,'SimulationCommand','connect')
set_param(gcs,'SimulationCommand','start')
wait(obj) %wait for timer object to stop running (occurs when simulation is finished)
end
In this example, you must stop the timer object using one of the "StopFcn" callback functions in the model. To do this, use the following command:
evalin('caller', 'stop(obj)')