Main Content

Prepare Model for Structured Text Generation

To generate structured text code from Simulink® models, Stateflow® charts, and MATLAB® functions, use the Simulink PLC Coder™ product. Prepare your model for structured text code generation by performing action such as setting the solver, identifying if your model is a single rate or multirate model, and checking model compatibility for structured text code generation.

Tasking Mode

If your Simulink model contains multirate signals, you must set the tasking mode. If your Simulink model does not contain multirate signals, proceed to solver selection.

Simulink PLC Coder generates code for single-tasking subsystems. For multirate subsystems, you must first explicitly set the tasking mode to single-tasking before selecting a solver. In the model configuration, on the Solver pane, clear the check box for Treat each discrete rate as a separate task.

Choose a Solver

Choose a solver for your Simulink PLC Coder model.

Model Solver Setting
Variable-stepUse a continuous solver. Configure a fixed sample time for the subsystem for which you generate code.
Fixed-stepUse a discrete fixed-step solver.

Configure Simulink Models for Structured Text Code Generation

This tutorial uses the example model plcdemo_simple_subsyystem.

  1. In the Command Window, enter the model name to open the model.

    openExample('plcdemo_simple_subsystem');

  2. Configure the model to use the fixed-step discrete solver. Click the solver link in the lower-right corner of the model window. In the Solver information pane, click View solver settings to open the Solver pane of the model configuration parameters. Under the Solver selection, set Type to Fixed-step and Solver to discrete (no continuous states).

    If your model uses a continuous solver and has a subsystem, configure a fixed sample time for the subsystem for which you generate code.

  3. Save this model as plcdemo_simple_subsystem1.

  4. Create a subsystem containing the components for which you want to generate structured text code.

    Optionally, rename In1 and Out1 to U and Y respectively, resulting in a subsystem like the following figure:

  5. Save the model with the new subsystem.

  6. In the top-level model, right-click the Subsystem block and select Block Parameters (Subsystem).

  7. In the Block Parameters block dialog box, select Treat as atomic unit.

  8. Click OK.

  9. Simulate, and then save your model.

You can now:

Verify System Compatibility for Structured Text Code Generation

You must already have a model configured to work with the Simulink PLC Coder software. If not, see Prepare Model for Structured Text Generation

  1. In your model, navigate to the subsystem for which you want to generate code.

  2. Click the subsystem block and in theApps tab, click PLC Coder. In the PLC Code tab, click Model Advisor. In the Model Advisor window , select all the Simulink PLC Coder checks, and click Run Checks.

    If the subsystem is not atomic, right-click the subsystem block and select PLC Code > Enable “Treat as atomic unit” to generate code.

    In the block parameter dialog box, select Treat as atomic unit.

Generate structured text code for your IDE. See Generate and Examine Structured Text Code.