Create and Use Code Generation Reports

Generate a Traceability Report from Configuration Parameters

To generate a Simulink® PLC Coder™ code generation report from the Configuration Parameters dialog box:

  1. Verify that the model is open.

  2. Open the PLC Coder app. Click the PLC Code tab.

  3. Click Settings and navigate to the PLC Code Generation pane.

  4. To enable report generation, select Report > Generate traceability report.

  5. Click Apply.

  6. Click Generate PLC Code to initiate code and report generation. The coder generates HTML report files as part of the code generation process.

    The HTML report appears:

For more information, see:

Keep the Report Current

If you generate a code generation report for a model, and then change the model, the report becomes invalid. To keep your code generation report current, after modifying the source model, regenerate code and the report. If you close and then reopen a model, regenerate the report.

Trace from Code to Model

You must have already generated code with a traceability report. If not, see Generate a Traceability Report from Configuration Parameters or Generate a Traceability Report from the Command Line.

To trace generated code to your model:

  1. In the generated code HTML report display, look for <S1>/Gain. Code Generation Report has syntax highlighting for easy readability. PLC-specific keywords are highlighted in blue, comments in green, and the rest of the code in black.

  2. In the HTML report window, click a link to highlight the corresponding source block. For example, in the HTML report shown in the previous figure, you click the hyperlink for the Gain block (highlighted) to view that block in the model. Clicking the hyperlink locates and displays the corresponding block in the model editor window. You can use the same method to trace other block from the HTML report.

Trace from Model to Code

You can select a component at any level of the model with model-to-code traceability. You can also view the code references to that component in the HTML code generation report. You can select the following objects for tracing:

  • Subsystem

  • Simulink block

  • MATLAB Function block

  • Truth Table block

  • State Transition Table block

  • Stateflow® chart, or the following elements of a Stateflow chart:

    • State

    • Transition

    • Graphical function

    • MATLAB® function

    • Truth table function

You must have already generated code with a traceability report to trace a model component to the generated code. If not, see Generate a Traceability Report from Configuration Parameters or Generate a Traceability Report from the Command Line.

To trace a model component to the generated code:

  1. Open the subsystem.

  2. Click PLC Code.

  3. Click the component to trace and in the Review Results section, click Navigate to Code.

  4. Selecting Navigate to Code activates the HTML code generation report. The following figure shows the result of tracing the Gain block within the subsystem.

    In the report, the highlighted tag S1/Gain indicates the beginning of the generated code for the block. You can use the same method to trace from other Simulink, Stateflow, and MATLAB objects to the generated traceability report.

For a programmatic way to trace a block in the model to generated code, see rtwtrace.

Model Web View in Code Generation Report

Model Web Views

To review and analyze the generated code, it is helpful to navigate between the code and model. You can include a web view of the model within the HTML code generation report. You can then share your model and generated code outside of the MATLAB environment. You need a Simulink Report Generator™ license to include a Web view (Simulink Report Generator) of the model in the code generation report.

Browser Requirements for Web Views

Web views require a web browser that supports Scalable Vector Graphics (SVG). Web views use SVG to render and navigate models.

You can use the following web browsers:

  • Mozilla® Firefox® Version 1.5 or later, which has native support for SVG. To download the Firefox browser, go to www.mozilla.com/.

  • Apple Safari Web browser

  • The Microsoft® Internet Explorer® web browser with the Adobe® SVG Viewer plugin. To download the Adobe SVG Viewer plugin, go to www.adobe.com/svg/.

    Note

    Web views do not currently support Microsoft Internet Explorer 9.

Generate HTML Code Generation Report with Model Web View

This example shows how to create an HTML code generation report which includes a web view of the model diagram.

  1. Open the plcdemo_simple_subsystem model.

  2. Open the PLC Coder app. Click the PLC Code tab.

  3. Click Settings and navigate to the Code Generation pane.

  4. To enable report generation, select Report > Generate traceability report.

  5. To enable model web view, select Report > Generate model web view.

  6. Click OK.

    The dialog box looks something like this figure:

  7. Click Generate PLC Code to initiate code and report generation. The code generation report for the top model opens in a MATLAB web browser.

  8. In the left navigation pane, select a source code file. The corresponding traceable source code is displayed in the right pane and includes hyperlinks.

  9. Click a link in the code. The model web view displays and highlights the corresponding block in the model.

  10. To go back to the code generation report for the top model, at the top of the left navigation pane, click the Back button until the report for the top model is displayed.

For more information about navigating between the generated code and the model diagram, see:

Model Web View Limitations

When you are using the model web view, the HTML code generation report includes the following limitations:

  • Code is not generated for virtual blocks. In the model web view, if you click a virtual block, the code generation report clears highlighting in the source code files.

  • Stateflow truth tables, events, and links to library charts are not supported in the model web view.

  • Searching in the code generation report does not find or highlight text in the model web view.

  • In a subsystem build, the traceability hyperlinks of the root-level inports and outports blocks are disabled.

  • If you navigate from the actual model diagram (not the model web view in the report), to the source code in the HTML code generation report, the model web view is disabled and not visible. To enable the model web view, open the report again, see Open Code Generation Report (Simulink Coder).

Generate a Static Code Metrics Report

The PLC Coder Static Code Metrics report provides statistics of the generated code. The report is generated when you select Generate Traceability Report in the Configuration Parameters dialog box. You can use the Static Code Metrics Report to evaluate the generated PLC code before implementation in your IDE. For more information, see Working with the Static Code Metrics Report.

The procedure is the same as generating the Traceability Report.

  1. Open the PLC Coder app. Click the PLC Code tab.

  2. Click Settings and navigate to the PLC Code Generation pane.

  3. To enable report generation, select Report > Generate traceability report.

  4. Click OK.

  5. Click Generate PLC Code to initiate code and report generation. The coder generates HTML report files as part of the code generation process. The Code Metrics Report is shown on the left navigation pane.

Generate a Traceability Report from the Command Line

To generate a Simulink PLC Coder code generation report from the command-line code for the subsystem, plcdemo_simple_subsystem/SimpleSubsystem:

  1. Open a Simulink PLC Coder model, for example:

    open_system('plcdemo_simple_subsystem');
  2. Enable the code generation parameter PLC_GenerateReport. To view the output in the model web view, also enable PLC_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.