Verify Generated Code by Using Code Tracing - MATLAB & Simulink (original) (raw)

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 use the PLC Coder App on an element in the model. This two-way navigation is bidirectional traceability.

Using code tracing, you can:

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 using the code generation report.

The generated code includes these resources that support code tracing:

Traceable Elements

Bidirectional traceability is supported for Simulink blocks and these Stateflow® elements:

Traceability in one direction is supported for these Stateflow elements:

For more information, see

Traceability in Generated Code

This example shows how to verify generated code by using the code generation report.

  1. Open the exampleGeneratingStructuredTextForAFeedForwardPIDControllerExample.
    openExample('plccoder/GeneratingStructuredTextForAFeedforwardPIDControllerExample')
  2. Open the app. Click the tab.
  3. Click Settings and navigate to the Code Generation pane.
  4. To enable report generation, select > .
  5. Click OK.
  6. Click to initiate code and report generation. The code generation report for the top model opens in a MATLAB® web browser.
  7. In the left navigation pane, select theplcdemo_pid_feedforward.exp file.
  8. Click a comment or line number hyperlink. The Simulink Editor displays and highlights the corresponding block in the model.
  9. To highlight the generated code for a block in the model, select the block and in the tab, click Navigate to Code. The generated code for the block is highlighted in the HTML code generation report.
  10. In the left navigation pane, you can click the Back button to go back to the previous code generation report.

Traceability Tags

A traceability tag appears in a comment above the corresponding line of generated code. The format of the tag is<_`system`_>/_`blockname`_.

This code shows a tag comment above the generated line of code. A Sum block within a subsystem one level below the root level of the source model generates this code:

(* Sum: '/Sum' *) rtb_Sum := In1 - In2;

Operator Traceability

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.

Operator Type Operators
Arithmetic +, -, *,/, %
+=, -=, *=,/=, %=
++, -- (prefix and postfix)
Logical !, &&,|
Relational ==, !=, <,>, <=, >=
Bit ~, |, ^,&, >>,<<
&=, ^=, |=,<<=, >>=
Conditional ?:

These operators are not supported.

Operator Type Operator Examples
Assignment operator =
Member of and pointer operators Array subscript: a[b]
Address of and pointer dereference: &a,*a
Member of: a.b, a->b
Other operators Parenthesis in function call: foo(a,b)
Comma: a, b
Scope resolution: a::b
Cast: type(a)
new, new[]
delete, delete[]

Generate a Traceability Report from the Command Line

To generate a Simulink PLC Coder™ code generation report from the command-line code for the subsystemplcdemo_simple_subsystem/SimpleSubsystem:

  1. Open a Simulink PLC Coder model, for example:
    openExample('plcdemo_simple_subsystem');
  2. Enable the code generation parameter PLC_GenerateReport. To view the output in the model web view, also enablePLC_GenerateWebview:
    set_param('plcdemo_simple_subsystem', 'PLC_GenerateReport', 'on');
    set_param('plcdemo_simple_subsystem', 'PLC_GenerateWebView', 'on');
  3. Generate the code.
    generatedfiles = plcgeneratecode('plcdemo_simple_subsystem/SimpleSubsystem')
    A traceability report is displayed. In your model, a View diagnostics hyperlink appears at the bottom of the model window. Click this hyperlink to open the Diagnostic Viewer window.
    If the model web view is also enabled, that view is displayed.

Traceability Limitations

These limitations apply to reports generated by the Simulink PLC Coder software:

See Also

Topics