| Contents | Index |
| On this page… |
|---|
Opening the Stateflow Debugger Setting Breakpoints to Debug Charts |
To open the debugger, use the menu item or enter the command-line function.
In the editor, select Debug > Stateflow Debugger.

At the MATLAB command line, enter sfdebugger.
During simulation, you can animate a chart in your model to provide visual verification that your chart behaves as expected. Animation highlights objects in a chart as execution progresses.
You can animate a chart during simulation in one of two contexts:
In normal mode on the host machine where you run MATLAB and Simulink software (see Animating Stateflow Charts in Normal Mode)
In external mode on a target machine where your generated code runs (see Animating Stateflow Charts in External Mode)
During simulation in normal mode on a host machine, you can animate states and transitions in a chart.
Open the chart you want to animate.
In the editor, select Debug > Stateflow Debugger to open the debugger.
In the Animation section of the debugger, select Enabled.
Control the speed of animation by entering a value in the Delay field:
For the fastest animation, select a value of 0 seconds.
For the slowest animation, select a value of 1 second.
To maintain highlighting of active states in the chart after simulation ends, select the Maintain Highlighting check box.
By default, active state highlighting disappears after chart simulation ends.
Start simulation.
The chart highlights states and transitions as they execute.
To remove highlighting of active states after simulation ends, select View > Remove Highlighting in the chart.
You can animate a chart in external mode — the mode in which Simulink Coder code generation software establishes communication between a Simulink model and code executing on a target system (see Data Exchange in the Simulink Coder documentation). In external mode, you can animate states in a chart, and view test point signals in a floating scope or signal viewer.
Animating States During Simulation in External Mode. To animate states in a chart in external mode:
Load the chart you want to animate to the target machine.
In the Stateflow Editor, select Debug > Stateflow Debugger to open the debugger.
In the Animation section of the debugger, select Enabled.
In the Stateflow Editor, select Simulation > Configuration Parameters.
In the left Select pane, select Code Generation > Interface.
In the Data exchange section of the right pane, select External mode from the drop-down menu in the Interface field and click OK.
In the Simulink model editor, select Tools > External Mode Control Panel.
In the External Mode Control Panel dialog box, click Signal & Triggering.
In the External Signal & Triggering dialog box, set these parameters:
| In: | Select: |
|---|---|
| Signal selection pane | Chart you want to animate |
| Trigger pane | Arm when connecting to target check box |
| Trigger pane | normal from drop-down menu in Mode field |
Build the model to generate an executable file.
Start the target in the background by typing this command at the MATLAB prompt:
!model_name.exe -w &
For example, if the name of your model is my_control_sys, enter this command:
!my_control_sys.exe -w &
In the Model Editor, select Simulation > External, and then select Simulation > Connect to Target.
Start simulation.
The chart highlights states as they execute.
Viewing Test Point Data in Floating Scopes and Signal Viewers. When you simulate a chart in external mode, you can view test point data in floating scopes and signal viewers. You can designate local data and states to be test points.
To view test point data during simulation in external mode:
Open the Model Explorer and for each data you want to view, follow these steps:
In the left Model Hierarchy pane, select the state or local data of interest.
In the right Dialog pane, select the Test point check box.
From a floating scope or signal viewer, click the signal selection button:
![]()
The Signal Selector dialog box opens.
In the Signal Selector Model hierarchy pane, select the chart.
In the Signal Selector List contents menu, select Testpointed/Logged signals only and then select the signals you want to view.
Simulate the model in external mode as described in Animating States During Simulation in External Mode.
The scope or viewer displays the values of the test point signals as the simulation runs.
For more information, see Behavior of Scopes and Viewers with Rapid Accelerator Mode in the Simulink documentation.
A breakpoint indicates a point at which the Stateflow debugger halts execution of a simulating chart. At this time, you can inspect Stateflow data and the MATLAB workspace to examine the status of a simulating chart.
The debugger supports global and local breakpoints. Global breakpoints halt execution on any occurrence of the specific type of breakpoint. Local breakpoints halt execution on a specific object.
Use the Breakpoint controls in the Stateflow debugger to specify global breakpoints. When a global breakpoint occurs during simulation, execution stops and the debugger takes control. Select any or all of these breakpoints:
Chart Entry — Simulation halts on any chart entry.
Event Broadcast — Simulation halts for any event broadcast.
State Entry — Simulation halts for any state entry.
Global breakpoints can be changed during run time and are immediately enforced. When you save the chart, all the Stateflow debugger settings (including breakpoints) are saved, so that the next time you open the model, the breakpoints are as you left them.
You can set local breakpoints for:
Charts
States
Transitions
Graphical functions
Truth table functions
Events
For graphical objects, you can set local breakpoints using the right-click context menu in the chart or the properties dialog box for that object. To set local breakpoints for events, you must use the properties dialog box because events are not graphical objects.
To set local breakpoints using the right-click context menu:
Right-click the graphical object (chart, state, transition, graphical function, or truth table function) and select Breakpoints.
Depending on the object that you right-click, you can set different breakpoints:
| For: | Select: |
|---|---|
| Charts | Chart Entry — Stop execution before entering the chart. |
| States | State Entry — Stop execution before performing the state entry actions. State During — Stop execution before performing the state during actions. State Exit — Stop execution before performing the state exit actions. |
| Transitions | When Tested — Stop execution before testing the transition to see if it is a valid path. When Valid — Stop execution after the transition tests valid, but before taking the transition. |
| Graphical or truth table functions | Function Call — Stop execution before calling the function. |
To set local breakpoints using the properties dialog box of the chart object:
Use one of the following tools to open the dialog box:
| Tool | Action |
|---|---|
| Stateflow Editor | For a chart, select File > Chart Properties. For a state, transition, graphical function, or truth table function, right-click the object and select Properties. |
| Model Explorer |
|
A dialog box appears for setting the properties of the object.
In the properties dialog box, select from the following breakpoint options:
| For: | Select: |
|---|---|
| Charts | On chart entry — Stop execution before entering the chart. |
| States | State During — Stop execution before performing the state during actions. State Entry — Stop execution before performing the state entry actions. State Exit — Stop execution before performing the state exit actions. |
| Transitions | When Tested — Stop execution before testing the transition to see if it is a valid path. When Valid — Stop execution after the transition tests valid, but before taking the transition. |
| Graphical or truth table functions | Function Call — Stop execution before calling the function. |
| Events | Start of Broadcast — Stop execution before broadcasting the event. End of Broadcast — Stop execution after a Stateflow object reads the event. |
To disable all breakpoints in the debugger, select the Disable all check box.
To find and clear all breakpoints without disabling them, use the Debug menu in the chart editor or enter Stateflow API commands.
How to Clear Breakpoints Using the Editor. In the chart editor, select Debug > Clear All Breakpoints.
How to Clear Breakpoints at the Command Line. Use the following Stateflow API commands. (For more information, see the Stateflow API documentation.)
% get a handle for the root object
rootObj = find(sfroot,'-isa','Stateflow.Machine','Name',model);
% find all states, transitions, data, events, and charts
stateObjects = rootObj.find('-isa','Stateflow.State');
transitionObjects =rootObj.find('-isa','Stateflow.Transition');
dataObjects = rootObj.find('-isa','Stateflow.Data');
eventObjects = rootObj.find('-isa', 'Stateflow.Event');
chartObjects = rootObj.find('-isa','Stateflow.Chart');
% for all states, clear their breakpoints
for i = 1:size(stateObjects,1)
stateObjects(i).Debug.Breakpoints.OnEntry = 0;
stateObjects(i).Debug.Breakpoints.OnDuring = 0;
stateObjects(i).Debug.Breakpoints.OnExit = 0;
stateObjects(i).Machine.Debug.BreakOn.ChartEntry = 0;
stateObjects(i).Machine.Debug.BreakOn.EventBroadcast = 0;
stateObjects(i).Machine.Debug.BreakOn.StateEntry = 0;
end
% for all transitions, clear their breakpoints
for i = 1:size(transitionObjects,1)
transitionObjects(i).Debug.Breakpoints.WhenTested = 0;
transitionObjects(i).Debug.Breakpoints.WhenValid = 0;
end
% for all data, clear their breakpoints
for i = 1:size(dataObjects,1)
dataObjects(i).Debug.Watch = 0;
end
% for all events, clear their breakpoints
for i = 1:size(eventObjects,1)
eventObjects(i).Debug.Breakpoints.StartBroadcast = 0;
eventObjects(i).Debug.Breakpoints.EndBroadcast = 0;
end
% for all charts, clear their breakpoints
for i = 1:size(chartObjects,1)
chartObjects(i).Debug.Breakpoints.OnEntry = 0;
end
The first command returns a handle to the machine object that represents the top level of the Stateflow hierarchy. The next five commands use the API method find to specify the type of object to find. For example, the command
stateObjects = rootObj.find(‘-isa','Stateflow.State')
searches through the rootObj and returns an array listing of all state objects in your model. (See Finding Objects and Properties in the Stateflow API documentation.)
You can also define the properties of Stateflow objects. For example, you can clear all breakpoints in your model by setting those property values to zero for all states, transitions, data, events, and charts as shown in the code.
You can enable debugging for all charts in a model or do so on a chart-by-chart basis.
Follow these steps when you want debugging to apply to all charts in a model:
Open the Configuration Parameters dialog box.
On the Simulation Target pane, select Enable debugging/animation.
If you open each chart in its own editor, you see that Debug > Enable Debugging is selected by default. To see instructions for library link charts, go to How to Enable or Disable Debugging for Library Link Charts.
Follow these steps when you want debugging to apply to only one chart:
Open the Configuration Parameters dialog box.
On the Simulation Target pane, select Enable debugging/animation.
Disable debugging for each chart that you do not want to debug:
Open the chart.
In the editor, clear Debug > Enable Debugging.
To see instructions for library link charts, go to How to Enable or Disable Debugging for Library Link Charts.
The sf_cdplayer model contains three charts:
UserRequest
CdPlayerModeManager
CdPlayerBehaviorModel
To enable debugging for only the CdPlayerModeManager chart:
Open the sf_cdplayer model.

Open the Configuration Parameters dialog box.
On the Simulation Target pane, select Enable debugging/animation.

This step enables debugging and animation for all charts in your model.
Disable debugging for the charts that you do not want to debug.
Open the UserRequest chart.

In the editor, clear Debug > Enable Debugging.
Open the CdPlayerBehaviorModel chart.

In the editor, clear Debug > Enable Debugging.
If you start simulation of sf_cdplayer in the Stateflow debugger, the debugger ignores execution of all charts except for CdPlayerModeManager. For more information, see Starting Simulation in the Debugger.
Whether you enable or disable debugging for a library link chart depends on the setting you specify for the chart in the library model. The Enable debugging/animation parameter in the Configuration Parameters dialog box does not control debugging preferences for library link charts.
To enable debugging for a library link chart in your model:
Open the library model.
Unlock the library.
Open the chart in that library.
Select Debug > Enable Debugging.
This step specifies that all linked instances of that library chart have debugging enabled.
To disable debugging for a library link chart in your model:
Open the library model.
Unlock the library.
Open the chart in that library.
Clear Debug > Enable Debugging.
This step specifies that all linked instances of that library chart have debugging disabled.
To debug the charts in a model, you start simulation in the debugger:
A debugging simulation session starts. When simulation reaches a breakpoint that you set, the Stateflow debugger appears as follows:

At the breakpoint, the following status items appear in the upper portion of the Debugger window:
Stopped — Displays the step executed just prior to breaking execution.
Executing — Displays the currently executing chart.
Current Event — Displays the event that the chart is processing.
Simulink Time — Displays the current simulation time.
During simulation, the chart is in read-only mode. The toolbar and menus change so that object creation is not possible. In this read-only mode, the chart is iced.
When the chart reaches a breakpoint, you can control the execution rate using single-step mode or continuous execution until the chart reaches another breakpoint. Use the following buttons in the Stateflow debugger to control the execution rate:
Continue — After simulation starts and the chart reaches a breakpoint, the Start button becomes Continue. Click Continue to continue simulation.
Step — Execute the next execution step, and suspend the simulation.
Break — Suspend the simulation and transfer control to the debugger.
Stop Simulation — Stop simulation and relinquish debugging control. When simulation stops, the Stateflow Editor toolbar and menus return to their normal appearance and operation so that object creation is again possible.
During single-step mode, the debugger does not zoom automatically to the chart object that is executing. Instead, the debugger opens the subviewer that contains that object. This behavior minimizes visual disruptions as you step through your analysis of a simulation.
The options in the Error checking options section of the Stateflow debugger insert generated code in the simulation target to provide breakpoints to catch different types of errors that might occur during simulation. Select any of the following error checking options:
State Inconsistency — Check for state inconsistency errors that are most commonly caused by the omission of a default transition to a substate in superstates with XOR decomposition. See State Inconsistencies in a Chart for a complete description and example.
Transition Conflict — Check whether there are two equally valid transition paths from the same source at any step in the simulation. See Conflicting Transitions in a Chart for a complete description and example.
Data Range — Check whether the minimum and maximum values you specified for a data in its properties dialog box are exceeded. Also check whether fixed-point data overflows its base word size. See Data Range Violations in a Chart for a complete description and example.
Detect Cycles — Check whether a step or sequence of steps indefinitely repeats itself. See Cyclic Behavior in a Chart for a complete description and example.
To include the supporting code designated for these debugging options in the simulation application, select the Enable debugging/animation check box in the Simulation Target pane of the Configuration Parameters dialog box. This option is described in Speeding Up Simulation.
You can enable animation of the chart to show which states and transitions execute during a particular time step. Use the following controls:
Animation — Select Enabled to turn on animation for the chart.
Delay — Enter the speed of animation for the chart: 0 for fastest animation and 1 for slowest animation.
Maintain Highlighting — Select this check box to maintain highlighting of active states at the end of chart simulation.
The options for Delay and Maintain Highlighting are available only when you enable animation. For more information, see Animating Stateflow Charts.
During simulation, the debugger monitors several execution indicators in the output display in the bottom pane of the debugger. You select the contents of this display with the following pull-down menus, which are available only after chart execution reaches a breakpoint.
Breakpoints — Display a list of the set breakpoints. You can set breakpoints in the debugger and in the properties dialog boxes of individual objects such as states, transitions, and functions. See Setting Breakpoints to Debug Charts for details. This option lists breakpoints for the currently executing chart or for all charts in the model.
Browse Data — Display the current values of defined data objects. This pull-down list lets you filter displayed data between all data and watched data. Watched data has the Data property Watch in Debugger enabled for it. Each of these categories is further filtered by data for the currently executing chart, or all charts in the model. For more details see Watching Data in the Stateflow Debugger.
Active States — Display a list of active states in the display area. Double-clicking any state causes the Stateflow Editor to display that state. This pull-down menu lets you display active states in the current chart, or active states for all charts in the model.
Call Stack — Display a sequential list of the Stopped and Current Event status items that occur with each single-step through the simulation.
After you make a selection, the pull-down menu for the current display appears highlighted. When you select an output display button, that type of output appears until you choose a different display type. You can clear the display by selecting File > Clear Display in the Stateflow debugger.
![]() | Debugging and Testing Stateflow Charts | Example of Debugging Run-Time Errors in a 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 |