MATLAB: Stateflow Stop Button

simulink

Is there a way to stop a simulation without leaving my Stateflow chart? Moreover, is there functionality for a push button or switch that will control a stop signal input to my chart?

Best Answer

Both of these requests can be accomplished as described below. The attached sample model, "stopStateflowExample.slx", provides an example for implementing these features.
1) Stop simulation within Stateflow diagram
This can be accomplished by creating a Simulink function in the Stateflow diagram that contains the "Stop Simulation" block connected to a constant block (with a positive value). When the logic of in the Stateflow diagram wants to stop the model, it simply calls this Simulink function. The attached model shows a simple implementation of this functionality.
The following documentation page gives further information on rules for using Simulink functions in Stateflow diagrams:
2) Push button to stop simulation
Simulink includes a Push Button block in the Simulink > Dashboard library. This button can be connected to a constant block, such that when the button is pushed and held, the constant block changes its value. The input of the constant block is then used as an input to the Stateflow diagram to as a stop signal. This simple behavior is shown in the example model. Please refer to the following documentation page for more information on the Push Button block:
Note that other blocks in the Dashboard library may be considered for this application, such as the Toggle Switch.