Skip to Main Content Skip to Search
Product Documentation

Using the Stateflow Debugger

Opening the Stateflow Debugger

To open the debugger, use the menu item or enter the command-line function.

How to Open the Debugger Using the Editor

In the editor, select Debug > Stateflow Debugger.

How to Open the Debugger at the Command Line

At the MATLAB command line, enter sfdebugger.

Animating Stateflow Charts

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:

Animating Stateflow Charts in Normal Mode

During simulation in normal mode on a host machine, you can animate states and transitions in a chart.

  1. Open the chart you want to animate.

  2. In the editor, select Debug > Stateflow Debugger to open the debugger.

  3. In the Animation section of the debugger, select Enabled.

  4. 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.

  5. 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.

  6. Start simulation.

    The chart highlights states and transitions as they execute.

  7. To remove highlighting of active states after simulation ends, select View > Remove Highlighting in the chart.

Animating Stateflow Charts in External Mode

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:

  1. Load the chart you want to animate to the target machine.

  2. In the Stateflow Editor, select Debug > Stateflow Debugger to open the debugger.

  3. In the Animation section of the debugger, select Enabled.

  4. In the Stateflow Editor, select Simulation > Configuration Parameters.

  5. In the left Select pane, select Code Generation > Interface.

  6. In the Data exchange section of the right pane, select External mode from the drop-down menu in the Interface field and click OK.

  7. In the Simulink model editor, select Tools > External Mode Control Panel.

  8. In the External Mode Control Panel dialog box, click Signal & Triggering.

  9. In the External Signal & Triggering dialog box, set these parameters:

    In:Select:
    Signal selection paneChart you want to animate
    Trigger paneArm when connecting to target check box
    Trigger panenormal from drop-down menu in Mode field

  10. Build the model to generate an executable file.

  11. 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 &

      Note   -w allows the target code to wait for the Simulink model connection.

  12. In the Model Editor, select Simulation > External, and then select Simulation > Connect to Target.

  13. 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:

  1. Open the Model Explorer and for each data you want to view, follow these steps:

    1. In the left Model Hierarchy pane, select the state or local data of interest.

    2. In the right Dialog pane, select the Test point check box.

  2. From a floating scope or signal viewer, click the signal selection button:

    The Signal Selector dialog box opens.

  3. In the Signal Selector Model hierarchy pane, select the chart.

  4. In the Signal Selector List contents menu, select Testpointed/Logged signals only and then select the signals you want to view.

  5. 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.

Setting Breakpoints to Debug Charts

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.

Setting Global Breakpoints

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:

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.

Setting Local Breakpoints

You can set local breakpoints for:

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:

  1. Right-click the graphical object (chart, state, transition, graphical function, or truth table function) and select Breakpoints.

  2. 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:

  1. Use one of the following tools to open the dialog box:

    ToolAction
    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.

     What if my chart objects are grouped?

    Model Explorer
    1. Show all Stateflow objects by selecting View > Row Filter > All Stateflow Objects.

    2. Right-click a chart, state, transition, graphical function, truth table function, or event and select Properties.

    A dialog box appears for setting the properties of the object.

  2. 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.

Disabling All Breakpoints

To disable all breakpoints in the debugger, select the Disable all check box.

Clearing All Breakpoints

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.

How to Enable Debugging for Charts

You can enable debugging for all charts in a model or do so on a chart-by-chart basis.

How to Enable Debugging for All Charts in a Model

Follow these steps when you want debugging to apply to all charts in a model:

  1. Open the Configuration Parameters dialog box.

  2. 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.

How to Enable Debugging for a Single Chart

Follow these steps when you want debugging to apply to only one chart:

  1. Open the Configuration Parameters dialog box.

  2. On the Simulation Target pane, select Enable debugging/animation.

  3. Disable debugging for each chart that you do not want to debug:

    1. Open the chart.

    2. 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.

Example of Configuring a Model to Debug a Single Chart

The sf_cdplayer model contains three charts:

To enable debugging for only the CdPlayerModeManager chart:

  1. Open the sf_cdplayer model.

  2. Open the Configuration Parameters dialog box.

  3. On the Simulation Target pane, select Enable debugging/animation.

    This step enables debugging and animation for all charts in your model.

  4. Disable debugging for the charts that you do not want to debug.

    1. Open the UserRequest chart.

    2. In the editor, clear Debug > Enable Debugging.

    3. Open the CdPlayerBehaviorModel chart.

    4. 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.

How to Enable or Disable Debugging for Library Link Charts

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:

  1. Open the library model.

  2. Unlock the library.

  3. Open the chart in that library.

  4. 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:

  1. Open the library model.

  2. Unlock the library.

  3. Open the chart in that library.

  4. Clear Debug > Enable Debugging.

    This step specifies that all linked instances of that library chart have debugging disabled.

Options for Controlling the Debugger

Starting Simulation in the Debugger

To debug the charts in a model, you start simulation in the debugger:

  1. Click the Start button.

    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:

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.

Options to Control Execution Rate in the Debugger

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:

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.

Options for Error Checking in the Debugger

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:

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.

Options to Control Chart Animation

You can enable animation of the chart to show which states and transitions execute during a particular time step. Use the following controls:

The options for Delay and Maintain Highlighting are available only when you enable animation. For more information, see Animating Stateflow Charts.

Options to Control the Output Display Pane

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.

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.

  


Free Stateflow Interactive Kit

Learn how engineers use Stateflow to model state machines in their Simulink models.


Get free kit

Trials Available

Try the latest version of Stateflow.


Get trial software
 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS