Start Simulink Simulation non-blocking with outputs
5 views (last 30 days)
My simulation runs fairly long and is blocking so there is no feedback. It is currently launched with this command:
app.simOut = sim(app.simIn);
There are other posts which indicate this command below is non-blocking, but it does not assign a simulation output
How do I start a simulation non-blocking AND get simOut when it is done?
The specific code I am trying to get to is of this functionality:
while(~constains(inputFromSTDOUT(),'Complete')) % Loop while not complete
fprintf('Text from STDOUT here...');
% Process simOut
X = simOut.SomeValue;
This code does not use/assign simOut so will not work as written... just written to form the question.
Peter O on 6 Jul 2021
Yes sim is a blocking command. You can configure a simulation to dump its output to the workspace and access it from there. Check the "Save to Workspace or File" options in the Data Import/Export tab of the Configuration Parameters window (Ctrl+E). To query the status of the simulation, consider using
NOT_READY = true;
% TIMEOUT Guard in case the simulation stalls or starts grinding. You can
% also set a similar timeout parameter with a set_param call or sim() call.
TIMEOUT = 10000;
while(NOT_READY && n < TIMEOUT)
state = get_param('mymodel', 'SimulationStatus')
n = n + 1;
NOT_READY = false;
% Grab the simout object from the base workspace and process it within the
SimData = evalin('base','SimOut')
Another option that might be a little more elegant with apps and would allow you to remove the polling loop is to make use of the event and listener structures in MATLAB. Register an event listener in your app and set up the model's StopFcn callback to trigger it. Then inspect the simulation state for errors, grab your data, and process. Simulink.SimulationOutput and Simulink.SimulationMetadata are two good classes to take a look through for helpful properties.