| Contents | Index |
| On this page… |
|---|
Basic Workflow for Using Traceability |
Traceability is the ability to navigate between a line of generated code and its corresponding object. For example, you can click a hyperlink in a traceability comment to go from that line of code to the object in the model. You can also right-click an object in your model to find the line in the code that corresponds to the object. This two-way navigation is known as bidirectional traceability.
See Code Tracing in the Embedded Coder documentation for information about how traceability works for Simulink blocks.
To enable traceability comments, you must have a license for Embedded Coder software. These comments appear only in code that you generate for an embedded real-time (ert) based target.
Bidirectional traceability is supported for these Stateflow objects:
States
Transitions
MATLAB functions
Truth Table blocks and truth table functions
Graphical functions
Simulink functions
Traceability in one direction is supported for these Stateflow objects:
Events (code-to-model)
Code-to-model traceability works for explicit events, but not implicit events. Clicking a hyperlink for an explicit event in the generated code highlights that item in the Contents pane of the Model Explorer.
Junctions (model-to-code)
Model-to-code traceability works for junctions with at least one outgoing transition. Right-clicking such a junction in the Stateflow Editor highlights the line of code that corresponds to the first outgoing transition for that junction.
Note MATLAB Function blocks that you insert directly in a Simulink model are also traceable. For more information, see Using Traceability in MATLAB Function Blocks in the Simulink documentation. |
Use traceability when you want to generate commented code for a large-scale model. You can identify chart objects in the code and avoid manually entering comments or descriptions.
Use traceability when you want to validate generated code. You can identify which chart object corresponds to a particular line of code and keep track of code from different objects that you have or have not reviewed.
The basic workflow for using traceability is:
Open your model, if necessary.
Define your system target file to be an embedded real-time (ert) target.
Enable and configure the traceability options.
Generate the source code and header files for your model.
Do one or both of these steps:
Trace a line of generated code to the model.
Trace an object in the model to a line of code.
You can see how bidirectional traceability works for states and transitions by following these steps:
Type old_sf_car at the MATLAB prompt.
Open the Configuration Parameters dialog box.
In the Code Generation pane, go to the Target selection section and enter ert.tlc for the system target file. Click Apply in the lower right corner of the window.
In the Code Generation > Report pane, select Create code generation report.
This step automatically selects Open report automatically and Code-to-model.
Select Model-to-code in the Navigation section. Then click Apply.
This step automatically selects all check boxes in the Traceability Report Contents section.
Go to the Code Generation > Interface pane. In the Software environment section, select continuous time. Then click Apply.
In the Code Generation pane, click Build in the lower right corner.
This step generates 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 automatically.
Click the old_sf_car.c hyperlink in the report.
Scroll down through the code to see the traceability comments.

Click the <S5>:2 hyperlink in this traceability comment:
/* During 'gear_state': '<S5>:2' */The corresponding state appears highlighted in the chart.
Click the <S5>:12 hyperlink in this traceability comment:
/* Transition: '<S5>:12' */The corresponding transition appears highlighted in the chart.
You can also trace an object in the model to a line of generated code. In the chart, right-click the object gear_state and select Code Generation > Navigate to Code.
The code for that state appears highlighted in old_sf_car.c.

In the chart, right-click the transition with the condition [speed > up_th] and select Code Generation > Navigate to Code.
The code for that transition appears highlighted in old_sf_car.c.

Note For a list of all Stateflow objects in your model that are traceable, click the Traceability Report hyperlink in the code generation report. |
You can see how bidirectional traceability works for a Truth Table block by following these steps:
Type sf_climate_control at the MATLAB prompt.
Complete steps 2 through 5 in Bidirectional Traceability for States and Transitions.
In the Code Generation pane of the Configuration Parameters dialog box, click Build in the lower right corner.
The code generation report appears automatically.
Click the sf_climate_control.c hyperlink in the report.
Scroll down through the code to see the traceability comments.

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.

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 Code Generation > Navigate to Code.
The code for that decision appears highlighted in sf_climate_control.c.

You can see how bidirectional traceability works for graphical functions by following these steps:
Type sf_clutch at the MATLAB prompt.
Complete steps 2 through 6 in Bidirectional Traceability for States and Transitions.
Go to the Solver pane in the Configuration Parameters dialog box. In the Solver options section, select Fixed-step in the Type field. Then click Apply.
In the Code Generation pane of the Configuration Parameters dialog box, click Build in the lower right corner.
The code generation report appears automatically.
Click the sf_clutch.c hyperlink in the report.
Scroll down through the code to see the traceability comments.

Click the <S1>:3 hyperlink in this traceability comment:
/* Graphical Function 'getSlipTorque': '<S1>:3' */In the chart, the graphical function getSlipTorque appears highlighted.
You can also trace a graphical function in the chart to a line of generated code. For example, right-click the graphical function detectSlip and select Code Generation > Navigate to Code.
The code for that graphical function appears highlighted in sf_clutch.c.

You can see how code-to-model traceability works for events by following these steps:
Type sf_security at the MATLAB prompt.
Complete steps 2 through 6 in Bidirectional Traceability for States and Transitions.
In the Code Generation pane of the Configuration Parameters dialog box, click Build in the lower right corner.
The code generation report appears automatically.
Click the sf_security.c hyperlink in the report.
Scroll down through the code to see the following traceability comment.

Click the <S8>:56 hyperlink in this traceability comment:
/* Event: '<S8>:56' */In the Contents pane of the Model Explorer, the event Sound appears highlighted.

You can see how model-to-code traceability works for junctions by following these steps:
Type sf_abs at the MATLAB prompt.
Complete steps 2 through 6 in Bidirectional Traceability for States and Transitions.
Go to the Solver pane in the Configuration Parameters dialog box. In the Solver options section, select Fixed-step in the Type field. Then click Apply.
In the Code Generation pane, click Build in the lower right corner.
The code generation report appears automatically.
Open the AbsoluteValue chart.
Right-click the left junction and select Code Generation > Navigate to Code.
The code for the first outgoing transition of that junction appears highlighted in sf_abs.c.

The format of a traceability comment depends on the Stateflow object type.
/* <ActionType> '<StateName>': '<ObjectHyperlink>' *//* During 'gear_state': '<S5>:2' */This comment refers to the during action of the state gear_state, which has the hyperlink <S5>:2.
/* Transition: '<ObjectHyperlink>' *//* Transition: '<S5>:12' */This comment refers to a transition, which has the hyperlink <S5>:12.
/* MATLAB Function '<Name>': '<ObjectHyperlink>' */Within the inlined code for a MATLAB function, comments that link to individual lines of the function have the following syntax:
/* '<ObjectHyperlink>' *//* 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 Function '<Name>': '<ObjectHyperlink>' */
Within the inlined code for a Truth Table block, comments for conditions, decisions, and actions have the following syntax:
/* Condition '#<Num>': '<ObjectHyperlink>' */ /* Decision 'D<Num>': '<ObjectHyperlink>' */ /* Action '<Num>': '<ObjectHyperlink>' */
<Num> is the row or column number that appears in the Truth Table Editor.
/* 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.
See Truth Table Block for syntax and examples.
/* Graphical Function '<Name>': '<ObjectHyperlink>' *//* Graphical Function 'hello': '<S1>:123' */This comment refers to a graphical function named hello, which has the hyperlink <S1>:123.
/* Simulink Function '<Name>': '<ObjectHyperlink>' *//* Simulink Function 'simfcn': '<S4>:10' */This comment refers to a Simulink function named simfcn, which has the hyperlink <S4>:10.
/* Event: '<ObjectHyperlink>' *//* Event: '<S3>:33' */This comment refers to an event, which has the hyperlink <S3>:33.
![]() | Generated Code Files for Targets You Build | Controlling Inlining of State Functions in Generated Code | ![]() |

Learn how engineers use Stateflow to model state machines in their Simulink models.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |