Main Content

Debug a StandaloneStateflowChart

A standalone Stateflow®chart is a MATLAB®class that defines the behavior of a finite state machine. Standalone charts implement classic chart semantics with MATLAB as the action language. You can program the chart by using the full functionality of MATLAB, including those functions that are restricted for code generation in Simulink®. For more information, seeCreate Stateflow Charts for Execution as MATLAB Objects.

To enable debugging, set a breakpoint in the standalone chart or in a MATLAB script that executes the chart. Breakpoints pause the execution of a chart. While the execution is paused, you can step through each action in the chart, view data values, and interact with the MATLAB workspace to examine the state of the chart.

Note

When debugging a standalone chart that you execute from a MATLAB script, first open the Stateflow Editor. Attempting to debug a standalone chart before opening the editor at least once can produce unexpected results.

For information on debugging Stateflow charts in Simulink models, seeSet Breakpoints to Debug Charts.

Set and Clear Breakpoints

Breakpoints appear as circular red badges. For example, this chart contains breakpoints on the stateAand on the transition fromAtoB.

Stateflow chart with breakpoints on a state and a transition.

You can set breakpoints on charts, states, and transitions.

Breakpoints on Charts

To set a breakpoint on a chart, right-click inside the chart and selectSet Breakpoint on Chart Entry. This type of breakpoint pauses the execution before entering the chart.

To remove the breakpoint, right-click inside the chart and clear theSet Breakpoint on Chart Entryoption.

Breakpoints on States and Transitions

You can set different types of breakpoints on states and transitions.

Object Breakpoint Type
State

On State Entry— Pause the execution before performing the stateentryactions.

During State— Pause the execution before performing the stateduringactions.

On State Exit— Pause the execution after performing the stateexitactions.

Transition

When Transition is Tested— Pause the execution before testing that the transition is a valid path. If no condition exists on the transition, this breakpoint type is not available.

When Transition is Valid— Pause the execution after the transition is valid, but before taking the transition.

To set a breakpoint on a state or transition, right-click the state or transition and selectSet Breakpoint. For states, the default breakpoints areOn State EntryandDuring State. For transitions, the default breakpoint isWhen Transition is Valid. To change the type of breakpoint, click the breakpoint badge and select a different configuration of breakpoints. For more information, seeManage Breakpoint Types and Conditions.

To remove the breakpoint, right-click the state or transition and selectClear Breakpoint. To remove all of the breakpoints in a chart, right-click inside the chart and selectClear All Breakpoints In Chart.

Manage Breakpoint Types and Conditions

A breakpoint badge can represent more than one type of breakpoint. To see a tooltip that lists the breakpoint types that are set on a state or transition, point to its badge. In this example, the badge on the stateArepresents two breakpoint types:On State EntryandDuring State.

工具提示显示断点状态类型.

To change the type of breakpoint on an object, click the breakpoint badge. In the Breakpoints dialog box, you can select a different configuration of breakpoints, depending on the object type. Clearing all of the check boxes in the Breakpoints dialog box removes the breakpoint.

Breakpoints dialog box for a state.

To limit the number of times that the execution stops at a breakpoint, add a condition to the breakpoint. By default, a Stateflow chart pauses whenever it reaches a breakpoint. When you add a condition to a breakpoint, the chart pauses at the breakpoint only when the condition is true. For example, with these conditions, the breakpoints on stateApause the execution of the chart only when the value ofxis negative.

Breakpoints dialog box showing conditions for a breakpoint.

To specify a condition for the breakpoint, you can use any valid MATLAB expression that combines numerical values and Stateflow data objects that are in scope at the breakpoint.

Control Chart Execution After a Breakpoint

When execution stops at a breakpoint, Stateflow enters debugging mode.

  • The MATLAB command prompt changes toK>>.

  • TheSymbolspane displays the value of each data object in the chart.

  • The chart highlights active elements in blue and the currently executing object in green.

For example, when the execution stops at the breakpoint in stateA, the border of the state and the first statement in the stateentryaction appear highlighted in green.

Stateflow chart that is paused at a breakpoint. The state A is active.

An execution status badge appears in the graphical object where execution pauses.

Badge Description

Execution is paused before entering a chart or in a stateentryaction.

Execution is paused in a stateduringaction.

Execution is paused in a stateexitaction.

Execution is paused before testing a transition.

Execution is paused before taking a valid transition.

When the chart is paused at a breakpoint, you can continue the execution by using:

  • Buttons in theDebugtab

  • The MATLAB Command Window

  • Keyboard shortcuts

Action Debug Tab Button MATLAB Command Keyboard Shortcut Description
Continue

dbcont Ctrl+T

Continue execution to the next breakpoint.

Step Over

dbstep F10

Advance to the next step in the chart execution. At the chart level, possible steps include:

  • Enter the chart

  • Execute a transition action

  • Activate a state

  • Execute a state action

For more information, seeExecution of a Stateflow Chart.

Step In

dbstep in F11

From a state or transition action that calls a function, advance to the first executable statement in the function.

From a statement in a function containing another function call, advance to the first executable statement in the second function.

Otherwise, advance to the next step in the chart execution. (See the Step Over option.)

Step Out

dbstep out Shift+F11

From a function call, return to the statement calling the function.

Otherwise, continue execution to the next breakpoint. (See the Continue option.)

Stop

dbquit Ctrl+Shift+T

Exit debug mode and interrupt the execution.

When you execute the standalone chart from the Stateflow Editor, this action removes the chart object from the MATLAB workspace.

In state or transition actions containing more than one statement, you can step through the individual statements one at a time by selectingStep Over. The Stateflow Editor highlights each statement before executing it.

Note

Because standalone charts define temporal logic in terms of wall-clock time, a temporal logic operator can become valid while a chart is paused at a breakpoint. In this case, the chart exits debugging mode and the execution continues to the next breakpoint.

Examine and Change Values of Chart Data

When Stateflow is in debug mode, theSymbolspane displays the value of each data object in the chart. You can also examine data values by pointing to a state or a transition in the chart. A tooltip displays the value of each data object used in the selected state or transition.

To test the behavior of your chart, in theSymbols窗格中,您可以更改一个数据对象的值during execution. Alternatively, at the debugging prompt, enter the new value by using the keywordthisin place of the chart object name. For instance, to change the value of the local datax, enter:

this.x = 7
The new value appears in theSymbolspane.

Note

When debugging a chart in a Simulink model, do not use the keywordthis. Instead, you can access all Stateflow data directly at the debugging prompt. For more information, seeView and Modify Data in the MATLAB Command Window.

Related Topics