Main Content

Trace Simulink Model Elements in Generated Code

To verify the generated code, Embedded Coder® provides bidirectional traceability between the Simulink® model and the generated code. You can use either method for traceability:

  • Code-to-model: The generated code displays these hyperlinks in comment lines in the generated code:

    • Block/subsystems names

    • Line numbers

    • Operators

    To highlight the corresponding block or subsystem in the Simulink Editor, click the hyperlinks.

  • Model-to-code: You can select single or multiple blocks of a model in the Simulink Editor and navigate to the corresponding generated code.


The Code view in code perspective provides bidirectional traceability by default.

Code-to-Model Traceability

To use hyperlinks for tracing code-to-model elements:

  1. Open the model and configure it as an ERT target.

  2. Build or generate code for the model. The Code view opens by default to display the generated code next to the model.

  3. In the generated code, place your cursor over the block names in comments to highlight source blocks. For example, generate an HTML report for the model rtwdemo_hyperlinks. In the generated code for the model step function in rtwdemo_hyperlinks.c, place your cursor over the UnitDelay: '<Root>/X' block. In the model window, the corresponding UnitDelay block is highlighted.

    Code view containing generated code is open next to the model. Mouse cursor is placed over a comment and the corresponding block is highlighted in the model.

To use line numbers for tracing code-to-model elements:

  1. In the previous model, rtwdemo_hyperlinks, click the hyperlink at line number 153. The line numbers can differ from the numbers that appear in your generated code.

  2. In the model window, the Chart subsystem is highlighted and contains the functionality on line 153.

    In the model, Stateflow chart is highlighted.

Traceability to Hidden Blocks

The code generator sometimes inserts hidden blocks during the code generation process for various reasons, for example, to maintain data integrity. Comments for the hidden blocks are included in the generated code. These comments, and the code generated from the hidden block, trace back to the original block in the model that triggered the insertion of the hidden block. Comments to hidden blocks follow this template:

/* <Hidden_Block_Type> generated from: <Hyperlinked_Original_Block_Name> */

For example,

/* SignalConversion generated from: '<Root>/Subsystem' */
  Varianttoground_B.VM_Conditional_Signal_Subsystem_0 = 0.0;
The hidden block SignalConversion is not present in the model. You can trace to the '<Root>/Subsystem' block in the model that triggered the insertion of the hidden block by clicking on the hyperlink on '<Root>/Subsystem'. For more information on this example, see Code Generation for Variant Blocks with One Variant Choice.

Model-to-Code Traceability

To trace model elements to their corresponding generated code:

  1. Complete steps 1 and 2 as in Code-to-Model Traceability

  2. Build or generate code for the model. The generated code is displayed in the Code view window next to the model.

  3. In the model window, click on a model element. To select multiple blocks, hold the SHIFT key and select additional blocks.

  4. In the generated code in Code view window, you see the first instance of highlighted code that is generated for the model element. At the top of the Code view, numbers that appear to the right of generated file names indicate the total number of highlighted lines in each file. This figure shows the result of tracing the Unit Delay block in model rtwdemo_hyperlinks.

    Code view containing generated code is open next to the model. Mouse cursor clicks a block in the model and corresponding lines of code are highlighted in Code view.

    At the top of the code window, use the navigation bar to move forward and backward through multiple instances of highlighted lines. Use the navigation sidebar to go directly to a line of code.

If you close and reopen a model, the Code view does not open until you open the Embedded Coder app. The Code view might not be available because Embedded Coder cannot find a build folder for your model in the current working folder. Reset the current working folder to the parent folder of the existing build folder.

If you are using the code generation report and want to reload existing traceability information, see Reload Existing Traceability Information.

Related Topics