Generate Structured Text Code from For Each Subsystems
You can use For Each subsystems to convert inefficient, repetitive algorithms into compact, easy-to-read code. For Each subsystems repeat execution during a simulation time step on each element or subarray of an input signal or mask parameter array.
This example shows how to generate structured text code from For Each subsystems.
Example Model
This example generates code from the ForEachScaleSignal
model.
open_system('ForEachScaleSignal.slx');
The model consists of a subsystem at the top level WeightsAndBiasesScaling
that accepts the inputs from the Input
, Slope
, and Common Offset
blocks.
The subsystem contains a For Each subsystem, Filter Signal
that performs these operations at every simulation time step:
Multiplies the
Input
andSlope
signalsAdds the
Common Offset
signal to the output of the multiplication of theInput
andSlope
signals.
This image shows the contents of the Filter Signal
subsystem.
Generate Structured Text Code
To generate structured text code using the Simulink® PLC Coder™ app:
In the PLC Coder tab, click PLC Code Generation Settings. Set the Target IDE to
3S CoDeSys 2.3
. Click OK.In the top level of the model, select the
WeightsAndBiasesScaling
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('ForEachScaleSignal/WeightsAndBiasesScaling');
Inspect Generated Code
Open the generated structured text file and view the generated code.
This text shows a snippet of the generated structured text code for the WeightsAndBiasesScaling
subsystem.
SS_STEP: (* Outputs for Atomic SubSystem: '<Root>/WeightsAndBiasesScaling' *) (* Outputs for Iterator SubSystem: '<S1>/Filter Signal' incorporates: * ForEach: '<S2>/For Each' *) FOR ForEach_itr := 0 TO 2 DO (* Product: '<S2>/Matrix Multiply' incorporates: * ForEachSliceSelector: '<S2>/ImpSel_InsertedFor_In1_at_outport_0' *) In1_0 := In1[ForEach_itr]; (* ForEachSliceAssignment: '<S2>/ImpAsg_InsertedFor_Scaled Signal_at_inport_0' incorporates: * ForEachSliceSelector: '<S2>/ImpSel_InsertedFor_In2_at_outport_0' * Inport: '<Root>/In2' * Product: '<S2>/Matrix Multiply' * Sum: '<S2>/Sum' *) FOR i := 0 TO 4 DO (* ForEachSliceSelector: '<S2>/ImpSel_InsertedFor_In2_at_outport_0' *) Out1_tmp := (3 * i) + ForEach_itr; Out1[Out1_tmp] := (In2[Out1_tmp] * In1_0) + In3; END_FOR; (* End of ForEachSliceAssignment: '<S2>/ImpAsg_InsertedFor_Scaled Signal_at_inport_0' *) END_FOR; (* End of Outputs for SubSystem: '<S1>/Filter Signal' *) (* End of Outputs for SubSystem: '<Root>/WeightsAndBiasesScaling' *) END_CASE;