| Stateflow® | ![]() |
| On this page… |
|---|
Opening the Stateflow® Debugger Setting Breakpoints for Debugging Setting Error Checking in the Debugging Window Starting Simulation in the Debugging Window |
To open the Stateflow® Debugging window, follow these steps:
During simulation, you can animate Stateflow charts in a Simulink® model to provide visual verification that your chart behaves as expected. Animation highlights objects in Stateflow charts as they execute. You can animate charts during simulation in 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 Stateflow chart. Follow these steps:
Open the Stateflow chart you want to animate.
In the Stateflow Editor, select Tools > Debug to open the Stateflow Debugging window.
In the Animation section of the Debugging window, select Enabled.
Control the speed of animation by entering a value in the Delay field, as follows:
For the fastest animation, select a value of 0 seconds.
For the slowest animation, select a value of 1 second.
Start simulation.
The Stateflow chart highlights states and transitions as they execute during simulation.
You can animate Stateflow charts in external mode — the mode in which Real-Time Workshop® code generation software establishes a communications link between a Simulink model and code executing on a target system (see External Mode in the Real-Time Workshop User's Guide). In external mode, you can animate states in Stateflow charts, and view test point signals in a floating scope or signal viewer.
Animating States During Simulation in External Mode. To animate states in Stateflow charts in external mode, follow these steps:
Load the Stateflow chart you want to animate to the target machine.
In the Stateflow Editor, select Tools > Debug to open the Stateflow Debugging window.
In the Animation section of the Debugging window, select Enabled.
In the Stateflow Editor, select Simulation > Configuration Parameters.
In the left Select pane, select Real-Time Workshop > 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 that contains the chart, select Tools > External Mode Control Panel.
In the External Mode Control Panel dialog box, click the Signals & Triggering button.
In the External Signal & Triggering dialog box, set these parameters:
| In: | Select: |
|---|---|
| Signal selection pane | Stateflow chart block 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 entering the following 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 Simulink model editor, select Simulation > External, and then select Simulation > Connect to Target.
Start simulation.
The Stateflow chart will highlight states as they execute.
Viewing Test Point Data in Floating Scopes and Signal Viewers. When you simulate Stateflow charts in external mode, you can view test point data in floating scopes and signal viewers. In Stateflow charts, you can designate local data and states to be test points.
To view test point data during simulation in external mode, follow these steps:
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 Stateflow chart.
In the Signal Selector Contents pane, list Testpointed signals only and check the test point 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.
A breakpoint indicates a point at which the Stateflow Debugging window halts execution of a simulating Stateflow chart. At this time, you can inspect Stateflow data and the MATLAB workspace and examine the status of a simulating Stateflow chart.
The Stateflow Debugging window 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 Debugging window to specify global breakpoints. When a global breakpoint is encountered during simulation, execution stops and the Debugger takes control. Select any or all of these breakpoints:
These breakpoints can be changed during run-time and are immediately enforced. When you save a Stateflow chart, the breakpoint settings are saved with it.
Global breakpoints can be changed during run-time and are immediately enforced. When you save the Stateflow chart, all the Stateflow Debugging window settings (including breakpoints) are saved, so that the next time you open the model, the breakpoints are as you left them.
You can set breakpoints for specific state actions, transitions, function calls, and event broadcasts in a Stateflow chart.
Open the properties dialog of the object for which you want to set a breakpoint, as follows:
In the properties dialog box, select from the following breakpoints options:
| For: | Select: |
|---|---|
| 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. |
| 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 window, select the check box Disable all.
There is no button or check box in the Debugger window to clear breakpoints. To find and clear all breakpoints without disabling them, you must use a set of Stateflow API commands as shown below. (See the Stateflow API documentation for more information.)
% get a handle for the root object
rootObject = find(sfroot,'-isa','Stateflow.Machine','Name',model);
% find all states, transitions, data, and charts
stateObjects = rootObject.find('-isa','Stateflow.State');
transitionObjects = rootObject.find('-isa','Stateflow.Transition');
dataObjects = rootObject.find('-isa','Stateflow.Data');
chartObjects = rootObject.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 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 four commands use the API method find to specify the type of object to find. For example, the command
stateObjects = rootObject.find(‘-isa','Stateflow.State')
searches through the rootObject 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, and charts as shown in the code.
The options in the Error checking options section of the Stateflow Debugging window insert generated code in the simulation target to provide breakpoints to catch different types of errors that might occur during simulation. Select any or all 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 Debugging State Inconsistencies 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 Debugging Conflicting Transitions for a complete description and example.
Data Range — Check whether the minimum and maximum values you specified for a data in its properties dialog are exceeded. Also check whether fixed-point data overflows its base word size. See Debugging Data Range Violations for a complete description and example.
Detect Cycles — Check whether a step or sequence of steps indefinitely repeats itself. See Debugging Cyclic Behavior 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 dialog. This option is described in Configuring the Simulation Target for the Main Model.
Note You must rebuild the target for any changes to any of the settings referenced above to take effect. |
To debug the Stateflow charts in a model, you start simulation in the Debugging window with these steps:
A debugging simulation session starts. When a breakpoint that you set is encountered, the Stateflow Debugging window takes on the following appearance:

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 Stateflow chart.
Current Event — Displays the event being processed by the Stateflow chart.
Simulink Time — Displays the current simulation time.
Code Coverage — Displays the percentage of code covered in this simulation.
During simulation, the Stateflow chart is marked read-only. The appearance of the Stateflow Editor toolbar and menus changes so that object creation is not possible. When the Stateflow Editor is in this read-only mode, its condition is referred to as iced.
Once you start simulation as described in Starting Simulation in the Debugging Window, and a breakpoint is reached, you can control the rate of execution of Stateflow charts to execute step-by-step or continuously until another breakpoint is reached. Use the following buttons in the Stateflow Debugging window to control the rate of execution:
Continue — After simulation has been started, and a breakpoint has been encountered, the Start button is marked Continue. Press Continue to continue simulation.
Step — Execute the next execution step, and suspend the simulation.
Break — Suspend the simulation and transfer control to the Debugging window.
Stop Simulation — Stop simulation altogether 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 simulation, the Debugging window monitors a variety of execution indicators in its output display in the bottom pane of the Debugging window. You select the contents of this display with the following pull-downs located just above the display, which are enabled only after a breakpoint is reached during simulation.
Breakpoints — Display a list of the set breakpoints. You can set breakpoints in the Debugger and in the properties dialogs of individual objects such as states, transitions, and functions. See Setting Breakpoints for Debugging 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 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.
Once you make a selection, the pull-down menu corresponding to the current display is highlighted. Once you select an output display button, that type of output is displayed until you choose a different display type. You can clear the display by selecting Clear Display from the File menu of the Stateflow Debugging window.
![]() | Debugging and Testing | Debugging Run-Time Errors Example | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |