| Contents | Index |
| On this page… |
|---|
Prepare the Chart Yourself or Use the Supplied Model Checking That Your Chart Conforms to Best Practices Setting the Length of the Simulation Configuring Animation for the Chart |
To prepare the chart for simulation yourself, work through the exercises in this section. Otherwise, open the supplied model to see how simulation parameters should appear. Enter this command at the MATLAB prompt:
run(docpath(fullfile(docroot,'toolbox','stateflow','gs','examples','Stage6Simulate.mdl')))
Before starting a simulation session, you should examine your chart to see if it conforms to recommended design practices:
A default transition must exist at every level of the Stateflow hierarchy that contains exclusive (OR) states (has exclusive [OR] decomposition). (See Deciding Where to Place Default Transitions.)
Whenever possible, input data objects should inherit properties from the associated Simulink input signal to ensure consistency, minimize data entry, and simplify maintenance of your model. Recall that in Defining the Inputs and Outputs, you defined the input temp to inherit its size and type from the Simulink output port temp, which provides the input value to the Air Controller chart.
Output data objects should not inherit types and sizes because the values are back propagated from Simulink blocks and may, therefore, be unpredictable. Recall that in Defining the Inputs and Outputs, you specified the data type as uint8 and the size as scalar (the default). (See Avoid inheriting output data properties from Simulink blocks in the Stateflow User's Guide.)
Tip You can specify data types and sizes as expressions in which you call functions that return property values of other variables already defined in Stateflow, MATLAB, or Simulink software. Such functions include type and fixdt. For more information, see Entering Expressions and Parameters for Data Properties in the Stateflow User's Guide. |
To specify the length of the simulation, follow these steps:
Open the model Stage5Trigger — either the one you created in the previous exercises or the supplied model for stage 5.
To open the supplied model, enter the following command at the MATLAB prompt:
run(docpath(fullfile(docroot,'toolbox','stateflow','gs','examples','Stage5Trigger.mdl')))
Save the model as Stage6Simulate in your local work folder.
Double-click Air Controller to open the chart.
Check the settings for simulation time:
In the Stateflow Editor, select Simulation > Configuration Parameters.
The Configuration Parameters dialog box opens on your desktop:

Click Solver in the left Select pane if it is not already selected.
Under Simulation time on the right, note that the start and stop times have been preset for you. You can adjust these times later as you become more familiar with the run-time behavior of the chart.
Keep the preset values for now and click OK to close the dialog box.
Leave the chart open for the next exercise.
When you simulate a Simulink model, you can animate Stateflow charts to highlight states and transitions as they execute. Animation provides visual verification that your chart behaves as expected. Animation is enabled by default, but you set the speed. To configure animation for your simulation session, follow these steps:
Verify that animation has been enabled for your chart:
In the Stateflow Editor, select Tools > Open Simulation Target.
The Simulation Target pane of the Configuration Parameters dialog box opens on your desktop.

Note This dialog box is used to configure Stateflow software for building targets. A target is a program that executes a Stateflow chart or a Simulink model that contains a Stateflow chart. Stateflow software builds a simulation target that you use to simulate your Stateflow application. For more information, see Building Targets in the Stateflow User's Guide. |
Verify that Enable debugging/animation is selected.
Close the dialog box.
Set the speed of animation:
From the Stateflow Editor, open the Stateflow debugger by selecting Debug > Stateflow Debugger or clicking the Debug icon:
![]()
The Stateflow debugger opens on your desktop:

By default, animation is enabled at 0-second delay.
Change the delay to 1 second so the animation will proceed at the slowest speed.
Leave the Air Controller chart and the debugger open for the next exercise.
In this exercise, you will learn how to set breakpoints in the debugger to pause simulation during key run-time activities so you can observe the behavior of your chart in slow motion. You can set the following breakpoints:
| Breakpoint | Description |
|---|---|
| Chart Entry | Simulation halts when the Stateflow chart wakes up. |
| Event Broadcast | Simulation halts when an event, such as SWITCH or CLOCK, occurs. |
| State Entry | Simulation halts when a state becomes active. |
You will also learn how to examine data values when simulation pauses.
Follow these steps:
In the debugger, select Chart Entry and State Entry as breakpoints.
Notice the Browse Data option in the menu bar just above the output display pane of the debugger:

The Browse Data option appears grayed out, but becomes active when simulation pauses at a breakpoint. You will use this option in Simulating the Air Controller Chart.
Leave the debugger open for the next exercise.
In this exercise, you will simulate the Air Controller chart. If this is the first time you are simulating the chart in the Stage6Simulate model, Stateflow software builds the simulation target by performing the following actions before simulation starts:
Parses the chart for state inconsistency errors, like those mentioned in Checking That Your Chart Conforms to Best Practices
Generates C code that represents the behavior of the chart
Builds the generated code into an executable program for the simulation target
Creates a new folder called slprj in the folder where the chart resides to store the generated files that make up the simulation target
Creates a MEX (MATLAB executable) file that corresponds to the C source file
The MATLAB command line displays status messages during each of these processes. You should see the following messages in your MATLAB Command Window, which indicate a successful build:

For more information, see Building Targets in the Stateflow User's Guide.
During simulation, you will change breakpoints and observe data values when execution pauses. Follow these steps:
In Stage6Simulate, open the Scope block. Position the Scope block, Air Controller chart, and debugger so all are visible on your desktop.
In the debugger, start simulation by clicking the Start button.
After the simulation target is built, the chart appears with a gray background, indicating that simulation has begun. Simulation continues until it reaches the first breakpoint, when the Air Controller chart wakes up. Notice that the status panel at the top of the debugger provides a snapshot of simulation activities at the breakpoint.
| Detail | What It Means | What You See at First Breakpoint |
|---|---|---|
| Stopped | What executed at the breakpoint | Entry: Chart Air Controller |
| Executing | Stateflow chart that is executing | Air Controller |
| Current Event | Event that is processed at this time step | Input event SWITCH |
| Simulink Time | Time at which the simulation paused | 0.000000 |
Note also that the Browse Data option is now enabled.
Click the down arrow to the right of the Browse Data option and select Watched Data (Current Chart) from the submenu.
By selecting this option, you can examine the values of the input temp and output airflow. Recall that you configured these objects as data to be watched in the debugger in the exercise Defining the Inputs and Outputs.
Tip You can also view data values from the MATLAB command line at simulation breakpoints. Here's how to do it:
|
Scroll down in the output display pane of the debugger to view the values of temp and airflow.
Note that temp is 70 (below the threshold for turning on FAN1) and airflow is 0 (indicating that no fans are running).
Resume simulation by clicking the Continue button.
Simulation continues until the next breakpoint, activation of the PowerOff state, which appears highlighted in the chart (as part of animation).

The default transition activates PowerOff after the chart wakes up.
Clear the breakpoint Chart Entry and continue simulation.
Simulation continues to the next breakpoint, the activation of the PowerOn state:

Note in the output display pane of the debugger that temp has risen to over 157 degrees. The Scope displays the temperature pattern:

Continue simulation through the following breakpoints, noting chart animation, Scope display, and how data values change:
| Breakpoint | Value
of temp (Degrees) | Value of airflow |
|---|---|---|
| Activation of FAN1 | > 157 | 0 |
| Default transition to FAN1.Off | > 157 | 0 |
| Activation of FAN2 | > 157 | 0 |
| Default transition to FAN2.Off | > 157 | 0 |
| Activation of SpeedValue | > 157 | 0 |
| Transition to FAN1.On (because temp >= 120 degrees) | > 157 | 0 |
| Transition to FAN2.On (because temp >= 150 degrees) | > 157 | 0 |
| Transition to FAN2.Off (because temp < 150 degrees) | > 149 and < 150 | 2 |
| Transition to FAN1.Off (because temp < 120 degrees) | > 119 and < 120 | 1 |
| Transition to FAN1.On (because temp >= 120 degrees) | > 120 | 0 |
| Transition to FAN1.Off (because temp < 120 degrees) | > 119 and < 120 | 1 |
To speed through the rest of the simulation, clear all breakpoints, change animation delay to 0, and click Continue.
Notice that FAN1 continues to cycle on and off as temp fluctuates between 119 and 120 degrees until power cycles off at 500 seconds. After power cycles off, the fans stop running and temp begins to rise unchecked until simulation reaches stop time at 600 seconds.
The Scope captures this activity:

Note This display should look the same as the Scope after running the prebuilt model in Running the Model. |
Save Stage6Simulate, and close all other windows and dialog boxes.
Where to go next. Now you are ready to use the debugger for detecting state inconsistencies and data range violations. See Debugging Common Modeling Errors.
![]() | Simulating the Chart | Debugging the Chart | ![]() |

Learn how engineers use Stateflow to model state machines in their Simulink models.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |