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 this option, 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 About 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.

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

Basic Workflow for Using Traceability

The workflow for using traceability is described in Trace Model Objects to Generated Code. Here are the basic steps:

  1. Open the MATLAB Function block in your Simulink model.

  2. Define your system target file to be an Embedded Real-Time (ERT) target.

    1. In the model, select Simulation > Model Configuration Parameters.

    2. In the Code Generation pane, enter ert.tlc for the system target file.

  3. Enable traceability options.

    1. In the Code Generation > Report pane, select Create code generation report.

      This action automatically selects the Open report automatically and Code-to-model options.

    2. Select Model-to-code.

      This action automatically selects all options in the Traceability Report Contents section.

  4. Generate the source code and header files for your model.

  5. Trace a line of code:

    To Trace:Do This:
    Line of source code to line of generated codeRight-click in a line in your source code and select Code Generation > Navigate to Code from the context menu
    Line of generated code to line of source codeClick a hyperlink in the traceability comment in your generated code

To learn how to complete each step in this workflow, see Tutorial: Using Traceability in a MATLAB Function Block

Tutorial: 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 eml_fire model. Follow these steps:

  1. Type eml_fire at the MATLAB prompt.

  2. In the Simulink model window, double-click the flame block to open the MATLAB Function Block Editor.

  3. In the Simulink model window, select Simulation > Model Configuration Parameters.

  4. In the Code Generation pane, go to the Target selection section and enter ert.tlc for the system target file. Then click Apply.

      Note:   Traceability comments appear hyperlinked in generated code only for embedded real-time (ert) targets.

  5. In the Code Generation > Report pane, select the Create code generation report option.

    This action automatically selects the Open report automatically and Code-to-model options.

  6. Select the Model-to-code option in the Navigation section. Then click Apply.

    This action automatically selects all options in the Traceability Report Contents section.

      Note:   For large models that contain over 1000 blocks, disable the Model-to-code option to speed up code generation.

  7. Go to the Code Generation > Interface pane. In the Software environment section, select the continuous time option. Then click Apply.

      Note:   Because this example model contains a block with a continuous sample time, you must perform this step before generating code.

  8. In the Code Generation pane, click Build in the lower right corner.

    This action generates source code and header files for the eml_fire model that contains the flame block. After the code generation process is complete, the code generation report appears automatically.

  9. Click the eml_fire.c hyperlink in the report.

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

    for (b_x = 0; b_x < 256; b_x++) {
               /* '<S2>:1:19' */
               /* '<S2>:1:21' */
               yb = loopVar_i + 2;
         
               /* '<S2>:1:22' */
               xb = b_x - 1;
    

      Note:   The line numbers shown above may differ from the numbers that appear in your code generation report.

  11. Click the <S2>:1:19 hyperlink in this traceability comment:

    /* '<S2>:1:19' */

    Line 19 of the function appears highlighted in the MATLAB Function Block Editor.

  12. You can also trace a line in a MATLAB function to a line of generated code. For example, right-click in line 21 of your function and select Code Generation > Navigate to Code from the context menu.

    The code location for line 21 appears highlighted in eml_fire.c.

Was this topic helpful?