Simulink Reference    
For Iterator

Implement a C-like for control flow statement in Simulink

Library

Ports & Subsystems/For Subsystem

Description


The For Iterator block, when placed in a subsystem, implements a C-like for control flow statement in Simulink as a For subsystem. In the For subsystem, the For Iterator block has iterative control over any Simulink blocks present. For each iteration value of the For Iterator block, the accompanying blocks execute. The number of iterations is set internally for the For Iterator block or externally with data input.

The following example shows a completed for control flow statement that increments an initial value of zero by 10 over 20 iterations.

In the preceding example, a For subsystem receives an input, which it passes to the For Iterator block inside. The For Iterator block uses this input to determine the number of times it executes the blocks of its subsystem, in this case, 20 times. Each time the blocks execute, a value of 10 is added to a sum, which is initially zero. In addition, for each time the blocks of the subsystem execute, the For Iterator block outputs a value equal to the number of times that the blocks have executed, including the current execution. This is referred to as the iterator value. This value, along with the sum value, is sent to an XY Graph block with the following result.

The preceding for control flow statement example can be represented by the following pseudocode.

You construct a For subsystem like the preceding example as follows:

  1. Create a subsystem and place a For Iterator block in it.
  1. This changes the subsystem icon to the text for{...}.

    You can use an ordinary subsystem or an atomic subsystem. In either case, the resulting For subsystem is atomic.

  1. Double-click the For Iterator block to open its Block Parameters dialog and enter as follows:

Data Type Support

The following rules apply to the data type of the number of iterations (N) input port:

Data output for the iterator value can be selected as double, int32, int16, or int8 in the Block Properties dialog.

Parameters and Dialog Box

States when starting
Set this field to reset if you want the states of the For subsystem to be reinitialized for each iteration. Otherwise, set this field to held (the default) to make sure that these subsystem states retain their values from the previous iteration.
Source of number of iterations
If you set this field to internal, the value of the Number of iterations field determines the number of iterations. If you set this field to external, the signal at the For Iterator block's N port determines the number of iterations.
Number of iterations
Set the number of iterations for the For Iterator block to this value. This field appears only if you selected internal for the Source of number of iterations field.
Show iteration number port
If this is selected, the For Iterator block outputs its iteration value. This value starts at 1 and is incremented by 1 for each iteration.
Output data type
Set the type for the iteration value output from the iteration number port to double, int32, int16, or int8.

Characteristics

Direct Feedthrough
No
Sample Time
Inherited from driving blocks
Scalar Expansion
No
Dimensionalized
No
Zero Crossing
No


  First-Order Hold For Iterator Subsystem