Main Content

Use Traceability in MATLAB Function Blocks

Extent of Traceability in MATLAB Function Blocks

Like other Simulink® blocks, MATLAB Function blocks support bidirectional traceability, but extend navigation to lines of source code. That is, you can navigate between a line of generated code and its corresponding line of source code. In other Simulink blocks, you can navigate between a line of generated code and its corresponding object.

In addition, you can select to include the source code as comments in the generated code. When you select MATLAB source code as comments parameter, the MATLAB® source code appears immediately after the associated traceability tag. For more information, see Include MATLAB Code as Comments in Generated Code.

For information about how traceability works in Simulink blocks, see Verify Generated Code by Using Code Tracing.

Traceability Requirements

To enable traceability comments in your code, you must have a license for Embedded Coder® software. These comments appear only in code that you generate for an Embedded Real-Time (ERT) target.


Traceability is not supported for MATLAB files that you call from a MATLAB Function block.

Using Traceability in a MATLAB Function Block

This example shows how to trace between source code and generated code in a MATLAB Function block in the DynamicIO model.

Open model DynamicIO.


1. Open the Embedded Coder app.

2. Change the system target file to ert.tlc when prompted. Traceability comments appear hyperlinked in generated code only for embedded real-time (ert.tlc) targets.

3. In the Configuration Parameters dialog box, on the Code Generation > Report pane, select these parameters, if not already selected:

  • Creat code generation report

  • Code-to-model

  • Model-to-code

4. In the Code Generation > Comments pane, select the MATLAB source code as comments and Stateflow object comments parameters. These parameters control different parts of the traceability comment.

5. In the model window, press Ctrl+B. This action generates source code and header files for the DynamicIO model that contains the emlHorizontalSum and emlVerticalSum blocks. After the code generation process is complete, the Code view appears automatically to the right of the model and displays the DynamicIO.c file.

6. Scroll down through the code to see the traceability comments, which appear as links inside /*...*/ brackets, as in this example.

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

/* '<S1>:1:2' y = sum(u,2); */

Line 2 of the function in the source code appears highlighted in the MATLAB Function Block Editor.

8. You can trace a line in a MATLAB function to lines of generated code. For example, right-click on line 2 of your function and select Code Generation > Navigate to Code from the context menu. The code location for line 2 appears highlighted in DynamicIO.c.

9. You can trace a line of generated code to a line of source code in a MATLAB function using the line number hyperlinks in the generated code.

Related Topics