Code tracing (traceability) uses hyperlinks to navigate between a line of generated code and its corresponding elements in a model. To find the lines of code and their corresponding elements, you can also right-click an element or elements in a model. This two-way navigation is bidirectional traceability.
Using code tracing, you can:
Verify that generated code is as you expect. You can identify which model elements correspond to a line of code. You can track code from different elements that you have or have not reviewed.
Verify that generated code meets the design requirements. You can link requirements to the model element and use code tracing to verify that the generated code for a model element meets the assigned requirements.
When you generate code from a Simulink® model, traceability information is embedded in the generated code, unless explicitly unspecified. The traceability information includes links for tracing between the generated source code and the model. You can view the generated code by either the:
The generated code includes resources that support code tracing:
Code element hyperlinks (indicated by underlining when you place your cursor over the code) to trace variables or types in the generated code to their declarations or definitions in the header files.
Tags in code comments that identify elements in a model from which lines of code are generated.
Line number hyperlinks that link to the model component from which the line of code was generated.
Bidirectional traceability is supported for Simulink blocks and these Stateflow® elements:
State transition tables
MATLAB functions. Traceability is not supported for external code that you call from a MATLAB function.
Truth table blocks
Traceability in one direction is supported for these Stateflow elements:
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.
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.
For more information, see Trace Stateflow Elements in Generated Code.
MATLAB Function blocks that you insert directly into a Simulink model are also traceable. For more information, see Use Traceability in MATLAB Function Blocks.
Open the model
Open the Embedded Coder® app. Build the model. On the C Code tab, click Build.
The HTML code generation report opens by default. To use the report, see Code Generation Report.
View the generated code in the Code view in the Code perspective.
Switch between the generated source files by using the drop-down list at the top of the Code view.
The source code contains traceability information such as hyperlinked comments, line numbers, variables, and operators. Place your cursor over or click a comment or line number hyperlink. The Simulink Editor highlights the corresponding block or blocks in the model.
To highlight the generated code for a block in the model, select the block. The generated code for the block is highlighted in the Code view.
To open the generated code for a referenced model, open the referenced model and view the generated code in the Code view.
Open the model
Open the Embedded Coder app. On the C Code tab, click Settings to open the Configuration Parameters dialog box. Select Create code generation report if it is not already selected. By default, Open report automatically, Code-to-model, and Model-to-code are selected.
If your model contains referenced models and you want to enable traceability for the referenced model's code generation report, repeat the previous step for each referenced model.
Build the model. On the C Code tab, click Build. The build process opens the code generation report in a MATLAB web browser.
In the left navigation pane, select a source code file. The source code and line numbers in the right pane contain hyperlinks to blocks in the model.
Click a comment or line number hyperlink. The Simulink Editor displays and highlights the corresponding block or blocks in the model.
To highlight the generated code for a block in the model, right-click the block and select C/C++ Code > Navigate to C/C++ Code. The generated code for the block is then highlighted in the HTML code generation report. To highlight the generated code for multiple blocks that you select, hold the SHIFT key, select multiple blocks, and then right-click any one block to select C/C++ Code > Navigate to C/C++ Code. The generated code for the blocks is then highlighted in the HTML code generation report.
If you have a referenced model in your model, in the left navigation pane, under Reference Models, click the link to a referenced model. The code generation report for the referenced model is now displayed in the MATLAB web browser.
In the left navigation pane, you can click the Back button to go back to the previous code generation report.
A traceability tag appears in a comment above the corresponding line of generated code.
The format of the tag is
is one of the following:
A unique system number assigned by the Simulink engine
is the name of the source
The code generator documents the tags for a model in the comments section of the
generated header file
. For example,
this comment appears in the header file for a model,
foo, that has a
Outer and a nested subsystem
/* Here is the system hierarchy for this model. * * <Root> : foo * <S1> : foo/Outer * <S2> : foo/Outer/Inner */
This code shows a tag comment above the generated line of code. A Gain block at the root level of a source model generates this code:
/* Gain: '<Root>/UnDeadGain1' */ rtb_UnDeadGain1_h = dead_gain_U.In1 * dead_gain_P.UnDeadGain1_Gain;
This code shows a tag comment above the generated line of code. A Gain block within a subsystem one level below the root level of the source model generates this code:
/* Gain: '<S1>/Gain' */ dead_gain_B.temp0 *= (dead_gain_P.s1_Gain_Gain);
The generated code provides traceability between operators in the generated code and Simulink blocks, Stateflow elements, or MATLAB Function blocks.
To verify the generated code by using operator traceability, in the generated code, click an operator hyperlink to highlight the source block in the model.
These operators are supported.
These operators are not supported.
|Operator Type||Operator Examples|
|Member of and pointer operators||Array subscript: |
|Address of and pointer dereference: |
|Member of: |
|Other operators||Parenthesis in function call: |
|Scope resolution: |
These limitations apply to reports generated by Embedded Coder software:
Under the following conditions, model-to-code traceability is disabled for a block if the block name contains:
A single quote (
An asterisk (
*) that causes a name-mangling ambiguity
relative to other names in the model. This name-mangling ambiguity occurs if in a
block name or at the end of a block name, an asterisk precedes or follows a slash
If a block name contains a newline character (
\n), the generated
code comment for the block path hyperlink replaces the newline character with a space
You cannot trace blocks representing these types of subsystems to generated code:
Nonvirtual subsystems for which code has been removed due to optimization
If you cannot trace a subsystem at subsystem level, you can trace individual blocks within the subsystem.
If you open a model on a platform that is different from the platform used to generate code, you cannot use model-to-code and code-to-model traceability.
Inline traceability is not available for files that are generated in