Is it possible to start and stop a Simulink simulation from inside a GUI?
9 views (last 30 days)
Show older comments
I would like to start and stop a Simulink simulation from inside a GUI.
Accepted Answer
MathWorks Support Team
on 22 Oct 2010
You can start and stop a simulation from a Graphical User Interface (GUI). Below are the necessary steps to create an example:
1. In this example, the following model is used:
start_and_stop.mdl
2. Create a GUI that contains a Start Simulation and Stop Simulation push button. Here is an example that was created using GUIDE, the MATLAB Graphical User Interface development environment:
start_and_stop_gui.m
start_and_stop_gui.fig
3. Program the CallBack of the Stop Simulation button to set a global variable that is used to determine if the Stop button has been pressed. This is already done in the start_and_stop_gui.m file; GUIStopFlag is used as the global variable. The code fragment reads as follows:
% --- Executes on button press in stopsim.
function stopsim_Callback(hObject, eventdata, handles)
% hObject handle to stopsim (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global GUIStopFlag;
GUIStopFlag = 1;
4. Program the CallBack of the Start Simulation button to start the simulation using the SIM command. This is also done in the start_and_stop_gui.m file. The code fragment reads as follows:
% --- Executes on button press in startsim.
function startsim_Callback(hObject, eventdata, handles)
% hObject handle to startsim (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
modelname = get(handles.modelname, 'String');
stoptime = str2num(get(handles.simstoptime, 'String'));
sim(modelname, [0 stoptime])
5. Write an S-function that returns 0 when GUIStopFlag is 0, and 1 when GUIStopFlag is 1. The output from this S-function, sysstop_new.m, is fed into a Stop Simulation block. The simulation will stop when GUIStopFlag is set to 1.
See:
sysstop_new.m
If you want to edit more simulation parameters and solver properties for the SIM command, consider using the SIMGET and SIMSET functions in step 4.
NOTE: This example is only one possible approach. The interesting part of this example is that it makes use of the built-in Stop Simulation block.
Alternatively, using SET_PARAM command from the MATLAB command prompt provides another approach to this problem. You can use the SET_PARAM function to start, stop, pause, continue a simulation, update a block diagram, or write all data logging variables to the base workspace. The format of the function call for this use is
set_param('sys', 'SimulationCommand', 'cmd')
where 'sys' is the name of the system and 'cmd' is 'start', 'stop', 'pause', 'continue', 'update', or 'WriteDataLogs'. This function call with the STOP option could be placed in the CallBack of the Stop Simulation button which would trigger the simulation to stop.
0 Comments
More Answers (0)
See Also
See Also
Categories
Find more on Programmatic Model Editing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!