Main Content

Generate Structured Text Code for Model Reference Hierarchy

Since R2025a

This example shows how to reference an existing model and generate IEC 61131-3 Structured Text code for the model reference hierarchy.

Create Model Reference Hierarchy

Open the models simple_sub1 and simple_sub2. These models represent a reusable algorithm. The simple_sub1 model consists of an atomic subsystem with one input and one output port.

open_system("simple_sub1")

The simple_sub2 model consists of an atomic subsystem with two input ports and one output port.

open_system("simple_sub2")

When using a referenced model:

  • To specify whether a referenced model can be referenced, set the Total number of instances allowed per model to One or Multiple.

  • To specify whether the referenced model can be referenced one or multiple times, set the Total number of instances allowed per top model as One or Multiple. By default, Total number of instances allowed per top model is set to Multiple.

Create a new model to be the top model in the model reference hierarchy. The top model uses Model blocks to refer to other models, and can include multiple instances of an individual referenced model. Add a Model block to the model. The new block is initially unresolved because it does not specify a referenced model. Double-click the Model block or right-click the Model block and select Block Parameters (ModelReference).

Enter the name of the referenced model in the Model name field, or click Browse and select the referenced model. For this example, click Browse and select simple_sub1. This name must contain fewer than 60 characters, not including the .slx suffix. Click OK. Add another Model block and select simple_sub2 as the referenced model. Combine the Model blocks as shown in this image.

Select the Model blocks and convert them into a Subsystem block. To convert the Subsystem block to an atomic subsystem, open the PLC Coder app and, in the PLC Code tab, click Convert to Atomic Subsystem. Add the required input and output ports. This image shows the completed model.

open_system("simple_top.slx")

Name the model as simple_top and save the model.

Generate Structured Text Code for Model Reference Hierarchy

In the Configuration Parameters window, in the Solver pane, ensure that the Type parameter is Fixed-step and the Solver parameter is Discrete (no continuous states) for the top model and the referenced models. To open the Configuration Parameters dialog boxes for the referenced models, double-click the Model blocks, to open the models and then, in the Modeling tab, select Model Settings.

To generate code, select the top-level subsystem in the simple_top model. In the PLC Code tab, click Settings > PLC Code Generation Settings. Change the Target IDE parameter to CODESYS 3.5. Click OK. Click Generate PLC Code.

Alternatively, you can generate code by using the plcgeneratecode function.

plcgeneratecode("simple_top/Subsystem")

Simulink PLC Coder™ generates the Structured Text code. The current folder now contains these new files and two new folders:

  • plcsrc — The folder that contains the Structured Text code for the top model

  • slprj > plc > R2025a > simple_sub1 — The folder that contains the generated code and other artifacts for the simple_sub1 referenced model

  • slprj > plc > R2025a > simple_sub2 — The folder that contains the generated code and other artifacts for the simple_sub2 referenced model

  • simple_top.slxc — The Simulink® cache file for the top model

  • simple_sub1.slxc and simple_sub2.slxc — The Simulink® cache files for the referenced models

View Generated Code in Code Generation Report

To view the generated code, in the PLC Code tab, Review Results section, click Open Report. The code generation report contains the code generated for the top-level model. The generated code for the reference model does not contain the global symbol definition.

In addition, the generated code includes a simple_top_symbols.exp file. This file contains the global symbol definition for both the top and reference models. To use the generated code, you must import the generated code files and the symbol file into your target IDE.

See Also

Topics