Documentation Center

  • Trial Software
  • Product Updates

Debug Run-Time Errors in a Chart

Create the Model and the Stateflow Chart

In this topic, you create a model with a Stateflow® chart to debug. Follow these steps:

  1. Create the following Simulink® model:

  2. Add the following states and transitions to your chart:

  3. In your chart, add an event Switch with a scope of Input from Simulink and a Rising edge trigger.

  4. Add a data Shift with a scope of Input from Simulink.

The chart has two states at the highest level in the hierarchy, Power_off and Power_on. By default, Power_off is active. The event Switch toggles the system between the Power_off and Power_on states. Power_on has three substates: First, Second, and Third. By default, when Power_on becomes active, First also becomes active. When Shift equals 1, the system transitions from First to Second, Second to Third, Third to First, for each occurrence of the event Switch, and then the pattern repeats.

In the model, there is an event input and a data input. A Sine Wave block generates a repeating input event that corresponds with the Stateflow event Switch. The Step block generates a repeating pattern of 1 and 0 that corresponds with the Stateflow data object Shift. Ideally, the Switch event occurs at a frequency that allows at least one cycle through First, Second, and Third.

Debug the Stateflow Chart

To debug the chart in Create the Model and the Stateflow Chart, follow these steps:

  1. Open the Model Configuration Parameters dialog box.

  2. In the Simulation Target pane, verify that Enable debugging/animation is selected.

  3. Click OK to close the Model Configuration Parameters dialog box.

  4. Open the Stateflow debugger.

  5. In the Breakpoints section, select the Chart Entry check box.

  6. Under Animation, select Enabled to enable animation of the chart during simulation.

  7. Click Start to start the simulation.

    Because you specified a breakpoint on chart entry, execution stops at that point and the debugger shows you informational messages.

  8. Click Step In.

    The Step In button executes the next step and stops.

  9. Continue clicking the Step In button and watching the animating chart.

    After each step, watch the chart animation and the debugger status area to see the sequence of execution.

Single-stepping shows that the chart does not exhibit the desired behavior. The transitions from First to Second to Third inside the state Power_on are not occurring because the transition from Power_on to Power_off takes priority. The output display of code coverage also confirms this observation.

Correct the Run-Time Error

In Debug the Stateflow Chart, you step through a simulation of a chart and find an error: the event Switch drives the simulation but the simulation time passes too quickly for the input data object Shift to have an effect.

Correct this error as follows:

  1. Stop the simulation so that you can edit the chart.

  2. Add the condition [t > 20.0] to the transition from Power_on to Power_off.

    Now the transition from Power_on to Power_off does not occur until simulation time is greater than 20.0.

  3. In the Stateflow debugger, click Start to begin simulation again.

  4. Click Step In repeatedly to observe the new behavior.

Identify Stateflow Objects in Error Messages

When an error message appears during simulation, the error refers to the relevant Stateflow object using its name and ID number. An example of an error message is: Unresolved event 'Switch' in transition Switch (#100).

The ID number of a Stateflow object is unique, but not its name. To identify an object using its ID number, enter the following Stateflow API commands at the MATLAB® prompt:

theObject = find(sfroot, 'Id', <id number>);

The first command finds the Stateflow object that matches the <id number> you specify. The second command highlights the chosen object in your chart. (See the Stateflow API documentation for information about the find and view methods.)

Was this topic helpful?