Start Simulink Simulation non-blocking with outputs
3 views (last 30 days)
Show older comments
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
set_param('main','SimulationCommand','start');
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:
set_param('main','SimulationCommand','start');
while(~constains(inputFromSTDOUT(),'Complete')) % Loop while not complete
pause(1);
fprintf('Text from STDOUT here...');
end
% Process simOut
X = simOut.SomeValue;
This code does not use/assign simOut so will not work as written... just written to form the question.
0 Comments
Answers (1)
Peter O
on 6 Jul 2021
Hi,
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;
n=1
set_param('mymodel','SimulationCommand','start')
while(NOT_READY && n < TIMEOUT)
state = get_param('mymodel', 'SimulationStatus')
n = n + 1;
if strcmpi(state,'stopped')
NOT_READY = false;
end
pause(1)
end
% Grab the simout object from the base workspace and process it within the
% app
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.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!