Products & Services Solutions Academia Support User Community Company

Learn more about Simulink   

For Iterator - Repeatedly execute contents of subsystem at current time step until iteration variable exceeds specified iteration limit

Library

Ports & Subsystems/For Iterator Subsystem

Description

The For Iterator block, when placed in a subsystem, repeatedly executes the contents of the subsystem at the current time step until an iteration variable exceeds a specified iteration limit. You can use this block to implement the block diagram equivalent of a for loop in the C programming language.

The output of a For Iterator subsystem can not be a function-call signal. Simulink software will display an error message if the simulation is run or the diagram updated.

The block's parameter dialog allows you to specify the maximum value of the iteration variable or an external source for the maximum value and an optional external source for the next value of the iteration variable. If you do not specify an external source for the next value of the iteration variable, the next value is determined by incrementing the current value:

in+1 = in +1

The model in the following figure uses a For Iterator block to increment an initial value of zero by 10 over 20 iterations at every time step.

The following figure shows the result.

The For Iterator subsystem in this example is equivalent to the following C code.

sum = 0;
iterations = 20;
sum_increment = 10;
for (i = 0; i < iterations; i++) {
	sum = sum + sum_increment;
}

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.

The following rules apply to the iteration variable input port.

Parameters and Dialog Box

States when starting

Set this field to reset if you want the states of the For subsystem to be reinitialized before the first iteration at each time step. Otherwise, set this field to held (the default) to make sure that these subsystem states retain their values from the last iteration at the previous time step.

Iteration limit source

If you set this field to internal, the value of the Iteration limit 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. The iteration limit source must reside outside the For Iterator subsystem.

Iteration limit

Set the number of iterations by specifying a number or a named constant. This field appears only if you selected internal for the Iteration limit source field. This parameter supports storage classes. You can define the named constant in the base workspace of the Model Explorer as a Simulink.Parameter object of the built-in storage class Define (custom) type. For more information, see Applying a Custom Storage Class to a Parameter in the Real-Time Workshop Embedded Coder documentation.

Set next i (iteration variable) externally

This option can be selected only if you select the Show iteration variable option. If you select this option, the For Iterator block displays an additional input for connecting an external iteration variable source. The value of the input at the current iteration is used as the value of the iteration variable at the next iteration.

Show iteration variable

If you select this check box, the For Iterator block outputs its iteration value.

Index mode

If you set this field to Zero-based, the iteration number starts at zero. If you set this field to One-based, the iteration number starts at one.

Iteration variable 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

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS