Main Content

Trace Stateflow Elements in Generated Code

To verify the generated code for your Stateflow® elements, you can trace Stateflow elements in your model to the generated code by using these types of navigation:

  • Code-to-model: Trace generated code back to the model by clicking hyperlinks in the comments or the hyperlinked line numbers, which highlights the corresponding model element in the Simulink Editor.

  • Model-to-code: Trace the model elements in the Simulink Editor to corresponding lines in generated code by right-clicking the model element and navigating to the generated code. This traceability is not supported for some Stateflow elements in Code view.

These examples illustrate how to trace different Stateflow elements.

Inline Traceability for Stateflow Elements

Inline traceability refers to the line-level traceability available in the generated code. You can click the hyper-linked line numbers to trace single or multiple Stateflow elements at the same time.

  1. Open the model.

    openExample('old_sf_car')
  2. Configure the model as an ERT target.

  3. On the Code Generation > Report pane, select these model configuration parameters:

  4. Select Code Generation > Comments > Stateflow object comments configuration parameter. This parameter enables the traceability comments for Stateflow elements.

  5. Go to the Code Generation > Interface pane. In the Software environment section, select continuous time. Click Apply. Before generating code, you must perform this step because this model contains a block with a continuous sample time.

  6. On the C Code tab, click Build.

    After the code generation process is complete, the code generation report appears.

  7. In the report, on the left navigation pane, click the old_sf_car.c hyperlink.

  8. To the view the inline traceability hyperlinks, scroll through the code. These line numbers can differ from the numbers that appear in your code generation report.

    Generated function for a Stateflow chart with a hyperlink to the shift_logic chart in the comment. Some line numbers are underlined as hyperlinks.

  9. Click the hyperlink on line number 190. The corresponding element is highlighted.

    The stateflow chart shift_logic with the first gear element highlighted.

Bidirectional Traceability for States and Transitions

  1. Open the model.

    openExample('old_sf_car')
  2. Complete steps 2 through 5 in Inline Traceability for Stateflow Elements.

  3. Press Ctrl+B to generate source code and header files for the old_sf_car model that contains the shift_logic chart. After the code generation process is complete, the code generation report appears.

  4. In the report, click the old_sf_car.c hyperlink.

View Results

  1. To see the traceability comments, scroll through the code. These line numbers can differ from the numbers that appear in your code generation report.

    Generated function for a Stateflow chart with hyperlinks in the comments for the gear states.

  2. Click the <S5>:2 hyperlink in this traceability comment:

    /* During 'gear_state': '<S5>:2' */

    The corresponding state appears highlighted in the chart.

  3. Click the <S5>:12 hyperlink in this traceability comment:

    /* Transition: '<S5>:12' */

    The corresponding transition appears highlighted in the chart. To remove highlighting from an element in the chart, press the Esc key.

  4. You can also trace elements in the model to lines of generated code. In the chart, right-click the element gear_state and select C/C++ Code > Navigate to C/C++ Code.

    The code for that state appears highlighted in old_sf_car.c.

    Generated function for the shift_logic chart with the comment for the gear state highlighted.

  5. In the chart, right-click the transition with the condition [speed > up_th] and select C/C++ Code > Navigate to C/C++ Code.

    The code for that transition appears highlighted in old_sf_car.c.

    Generated code with four lines for a state transition highlighted.

    Note

    For a list of the Stateflow elements in your model that are traceable, click the Traceability Report hyperlink in the code generation report.

Bidirectional Traceability for State Transition Tables

This example shows how to navigate bidirectionally between elements in a state transition table and the generated C/C++ and HDL code for traceability.

  1. Open the model. This model is already configured for traceability.

    openExample('sf_cdplayer_STT')
  2. Press Ctrl+B to generate source code and header files for the sf_cdplayer_STT model. After the code generation process is complete, the code generation report appears.

  3. Click the sf_cdplayer_STT.c hyperlink in the report.

  4. To see the traceability comments, scroll through the code. The line numbers shown can differ from the numbers that appear in your code generation report.

    Generated function for a state transition table with hyperlinks in the comments and some structures.

  5. Click the <S2>:58 hyperlink in this traceability comment:

    /* Entry Internal 'ON': '<S2>:58' */

    The corresponding state'ON' appears highlighted in the state transition table.

    State transition table with the state On highlighted.

  6. You can trace a state or transition from the state transition table to the generated code. Right-click the state Standby and select C/C++ Code > Navigate to C/C++ Code.

    The entry code for the state Standby is highlighted in the generated code.

    Generated code with six lines for the Standby state highlighted.

Bidirectional Traceability for Truth Table Blocks

  1. Open the model.

    openExample('sf_climate_control')
  2. Complete steps 2 through 5 in Inline Traceability for Stateflow Elements.

  3. To build the model, press Ctrl+B.

  4. In the code generation report, click the sf_climate_control.c hyperlink.

  5. To see the traceability comments, scroll through the code. These line numbers can differ from the numbers that appear in your code.

    Generated code with hyperlinks in the comments.

  6. Click the <S1>:1:47 hyperlink in this traceability comment:

    /* Action '3': '<S1>:1:47' */

    In the Truth Table Editor, row 3 of the Action Table appears highlighted.

    Action table with row 3 highlighted.

  7. You can also trace a condition, decision, or action in the table to a line of generated code. For example, right-click a cell in the column D2 and select C/C++ Code > Navigate to C/C++ Code.

    The code for that decision appears highlighted in sf_climate_control.c.

    Generated code with two lines for a decision highlighted.

    Tip

    To select C/C++ Code > Navigate to C/C++ Code for a condition, decision, or action, right-click a cell in the row or column that corresponds to that truth table element.

Bidirectional Traceability for Graphical Functions

  1. Open the model.

    openExample("stateflow/ClutchWithEnabledSubsystemsExample")
  2. Complete steps 2 through 5 in Inline Traceability for Stateflow Elements.

  3. In the Configuration Parameters dialog box, go to the Solver pane. In the Solver selection section, select Fixed-step in the Type field. Click Apply. Before generating code, you must perform this step because the model does not work with variable-step solvers.

  4. To build the model, press Ctrl+B.

  5. In the code generation report, click the sf_clutch_enabled_subsystems.c hyperlink.

  6. To see the traceability comments, scroll through the code. These line numbers can differ from the numbers that appear in your code generation report.

    Generated code comments describing elements in the model.

  7. Click the <S1>:3 hyperlink in this traceability comment:

    /* Graphical Function 'getSlipTorque': '<S1>:3' */

    In the chart, the graphical function getSlipTorque appears highlighted.

  8. You can trace a graphical function in the chart to a line of generated code. For example, right-click the graphical function detectSlip and select C/C++ Code > Navigate to C/C++ Code.

    The code for that graphical function appears highlighted in sf_clutch_enabled_subsystems.c.

    Generated code with five lines for a graphical function highlighted.

Code-to-Model Traceability for Events

  1. Open the model.

    openExample('sf_security')
  2. Complete steps 2 through 5 in Inline Traceability for Stateflow Elements.

  3. To build the model, press Ctrl+B.

  4. In the code generation report, click the sf_security.c hyperlink.

  5. To see this traceability comment, scroll through the code. These numbers can differ from the numbers that appear in your code generation report.

    Generated code with traceability comments that identify an entry and an event in the model.

  6. Click the <S3>:56 hyperlink in this traceability comment:

    /* Event: '<S3>:56' */

    In the Contents pane of the Model Explorer, the event Sound appears highlighted.

    Model Explorer with the Security System chart selected in the left pane and the Sound event highlighted in the center pane.

Model-to-Code Traceability for Junctions

  1. Open the model.

    openExample('ex_if_else_SF')
  2. Complete steps 2 through 5 in Inline Traceability for Stateflow Elements.

  3. To build the model, press Ctrl+B.

  4. In the code generation report, open the Stateflow chart named Chart.

  5. Right-click the top-left junction and select C/C++ Code > Navigate to C/C++ Code.

    The code for the first outgoing transition of that junction appears highlighted in ex_if_else_SF.c.

    Generated code with two lines for the transition highlighted.

Format of Traceability Comments for Stateflow Elements

The format of a traceability comment depends on the Stateflow element type.

State

Syntax

/* <ActionType> '<StateName>': '<elementHyperlink>' */

Example

/* During 'gear_state': '<S5>:2' */

This comment refers to the during action of the state gear_state, which has the hyperlink <S5>:2.

Transition

Syntax

/* Transition: '<elementHyperlink>' */

Example

/* Transition: '<S5>:12' */

This comment refers to a transition, which has the hyperlink <S5>:12.

MATLAB Function

Syntax

/* MATLAB Function '<Name>': '<elementHyperlink>' */

Within the inlined code for a MATLAB® function, comments that link to individual lines of the function have this syntax:

/* '<elementHyperlink>' */

Examples

/* MATLAB Function 'test_function': '<S50>:99' */
/* '<S50>:99:20' */

The first comment refers to the MATLAB function named test_function, which has the hyperlink <S50>:99.

The second comment refers to line 20 of the MATLAB function in your chart.

Truth Table Block

Syntax

/* Truth Table Function '<Name>': '<elementHyperlink>' */

Within the inlined code for a Truth Table block, comments for conditions, decisions, and actions have this syntax:

/* Condition '#<Num>': '<elementHyperlink>' */
/* Decision 'D<Num>': '<elementHyperlink>' */
/* Action '<Num>': '<elementHyperlink>' */

<Num> is the row or column number that appears in the Truth Table Editor.

Examples

/* Truth Table Function 'truth_table_default': '<S10>:100' */
/* Condition '#1': '<S10>:100:8' */
/* Decision 'D1': '<S10>:100:16' */
/* Action '1': '<S10>:100:31' */

The first comment refers to a Truth Table block named truth_table_default, which has the hyperlink <S10>:100.

The other three comments refer to elements of that Truth Table block. Each condition, decision, and action in the Truth Table block has a unique hyperlink.

Truth Table Function

For syntax and examples, see Truth Table Block.

Graphical Function

Syntax

/* Graphical Function '<Name>': '<elementHyperlink>' */

Example

/* Graphical Function 'hello': '<S1>:123' */

This comment refers to a graphical function named hello, which has the hyperlink <S1>:123.

Simulink Function

Syntax

/* Simulink Function '<Name>': '<elementHyperlink>' */

Example

/* Simulink Function 'simfcn': '<S4>:10' */

This comment refers to a Simulink® function named simfcn, which has the hyperlink <S4>:10.

Event

Syntax

/* Event: '<elementHyperlink>' */

Example

/* Event: '<S3>:33' */

This comment refers to an event, which has the hyperlink <S3>:33.

Related Topics