Main Content

Generate Structured Text Code for Width Blocks

The Width block is commonly used with the For Each subsystem block to compute the signal width inside the for-each loop. This example shows how to generate structured text code for a subsystem that has a Width block.

Example Model

This example generates code for the ForEachandWidthModel model.

open_system('ForEachandWidthModel.slx')

The model consists of a Tiling subsystem at the top level that accepts an input signal from the Coefficients block.

The Tiling subsystem contains two For Each subsystem blocks that break the input ten-by-ten matrix into a two-by-ten matrix and then into a two-by-two matrix. The Width block inside the Dim2 For Each subsystem block displays the output width of the input vector.

Generate Structured Text Code

To generate structured text code using the Simulink® PLC Coder™ app:

  1. In the PLC Coder tab, click Settings > PLC Code Generation Settings. Set Target IDE to 3S CoDeSys 2.3. Click OK.

  2. In the top level of the model, select the Tiling subsystem block. In the PLC Code tab, click Generate PLC Code.

Alternatively, to generate structured text code from the MATLAB command line, use the plcgeneratecode function.

generatedfiles = plcgeneratecode('ForEachandWidthModel/Tiling');

Inspect Generated Code

Open the generated structured text file and view the generated code.

The Width block output shows up on this line as 4.0

rtb_Sum := ((((UUU[rtb_Sum_tmp + 1] + UUU[rtb_Sum_tmp]) + UUU[rtb_Sum_tmp_tmp])...
 + UUU[rtb_Sum_tmp_tmp + 1]) / 4.0) + UnitDelay_DSTATE[(ForEach_itr_d * 5) + ForEach_itr];               

See Also

Related Topics