Skip to Main Content Skip to Search
Home |   Select Country  Choose Country  |  Contact Us  |  Cart Store 
Create Account | Log In
Products & Services Solutions Academia Support User Community Company
spacer spacer spacer spacer spacer spacer

Technical Solutions

Is it possible to start and stop a Simulink simulation from inside a GUI?


Date Last Modified: Friday, June 26, 2009
Solution ID:   1-15M12
Product:   Simulink
Reported in Release:   R13
Platform:   All Platforms
Operating System:   All OS
 

Subject:

Is it possible to start and stop a Simulink simulation from inside a GUI?

Problem Description:

I would like to start and stop a Simulink simulation from inside a GUI.

Solution:

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:

http://www.mathworks.com/support/solutions/files/s2992/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:

http://www.mathworks.com/support/solutions/files/s2992/start_and_stop_gui.m

http://www.mathworks.com/support/solutions/files/s2992/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. Please see:

http://www.mathworks.com/support/solutions/files/s2992/sysstop_new.m

If you want to edit more simulation parameters and solver properties for the SIM command, please 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.

 

Related Solutions:

Please provide feedback to help us improve this Solution
Contact support
E-mail this page
Print this page