Documentation Center

  • Trial Software
  • Product Updates

For Iterator

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

Library

Ports & Subsystems

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

Suppose that you have the following modelmodel:

Over 20 iterations, the For Iterator block increments a value by 10 at each 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;
}

    Note   Placing a For Iterator block in a subsystem makes it an atomic subsystem if it is not already an atomic subsystem.

Data Type Support

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

  • The input port accepts data of mixed numeric types.

  • If the input port value is noninteger, it is first truncated to an integer.

  • Internally, the input value is cast to an integer of the type specified for the iteration variable output port.

  • If no output port is specified, the input port value is cast to type int32.

  • If the input port value exceeds the maximum value of the output port's type, it is truncated to that maximum value.

Data output for the iterator value can be selected as double, int32, int16, or int8 in the block parameters dialog box.

The following rules apply to the iteration variable input port:

  • It can appear only if the iteration variable output port is enabled.

  • The data type of the iteration variable input port is the same as the data type of the iteration variable output port.

For more information, see Data Types Supported by Simulink in the Simulink documentation.

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 Apply Custom Storage Classes to Parameters in the 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

Was this topic helpful?