Main Content

Generate and Examine Structured Text Code

To generate structured text code from Simulink® models, Stateflow® charts, and MATLAB® functions, use the Simulink PLC Coder™ product. Use the generated structured text code in applications such as rapid prototyping, control algorithm validation, and test bench verification.

Generate Structured Text from the Model Window

To generate structured text code from your Simulink model, complete the steps to prepare your model for structured text code generation. For more information, see Prepare Model for Structured Text Generation. This tutorial uses the plcdemo_simple_subsystem.

  1. In the Command Window, enter:

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

  3. Click Settings.

  4. On the PLC Code Generation pane, select an option from the Target IDE list, for example, CODESYS 2.3.

    The default Target IDE list displays the full set of supported IDEs. To see a reduced subset of the target IDEs supported by Simulink PLC Coder, clear the Show full target list check box. To customize this list, use the plccoderpref function.

  5. Click OK.

  6. Click Generate PLC Code to:

    • Generate structured text code.

    • Store generated code in model_name.exp (for example, plcdemo_simple_subsystem.exp)

    When code generation is complete, a View diagnostics hyperlink appears at the bottom of the model window. Click this hyperlink to open the Diagnostic Viewer window.

    This window has links that you can click to open the associated files. For more information, see Files Generated by Simulink PLC Coder.

Generate Structured Text Through the MATLAB Interface

You can generate structured text code for a subsystem in the Command Window by using the plcgeneratecode function. You must have already configured the parameters for the model or, alternatively, you can use the default settings.

For example, to generate code from the SimpleSubsystem subsystem in the plcdemo_simple_subsystem model:

  1. Open the plcdemo_simple_subsystem model:


  2. Open the Configuration Parameters dialog box by using the plcopenconfigset function:


  3. Select a target IDE.

  4. Configure the subsystem by preparing your model for structured text code generation. For more information, see Prepare Model for Structured Text Generation.

  5. Generate code for the subsystem:

    generatedfiles = plcgeneratecode('plcdemo_simple_subsystem/SimpleSubsystem')

When using plcgeneratecode for code generation, all diagnostic messages are printed to the MATLAB Command Window.

View Generated Code

After generating the code, you can view it in the MATLAB editor. For a description of how the generated code for the Simulink components map to structured text components, see Verify Generated Code by Using Code Tracing. You can view:

  • Matrix data types: Simulink PLC Coder converts matrix data types to single-dimensional vectors (column-major) in the generated structured text.

  • Generated code header: If your model has author names, creation dates, and model descriptions, the generated code contains these items in the header comments. The header also lists fundamental sample times for the model and the subsystem block for which you generate code.

  • Code comments: You can choose to propagate block descriptions to comments in generated code. See Propagate Block Descriptions to Code Comments.

This figure illustrates generated code for the CODESYS Version 2.3 PLC IDE. Generated code for other platforms, such as Rockwell Automation® RSLogix™ 5000, is in XML or another format.

Once you are satisfied with the generated structured text code, optionally change your workflow to automatically generate and import code to the target IDE. For more information, see Import Structured Text Code Automatically.