Documentation

While Iterator

Repeat execution of a subsystem while a logical condition signal is true

  • Library:
  • Ports & Subsystems

Description

When placed in a subsystem block, a While Iterator block controls execution of the subsystem. During a simulation time step, this block repeats execution of the subsystem while an input logical (Boolean) condition signal to the block is true (1).

Placing a While Iterator block in a Subsystem block makes it an atomic subsystem.

Ports

Input

expand all

Signal with the result from evaluating a logical condition. Because the subsystem is not externally triggered during a time step, evaluating a condition as true (1) or false (0) must reside within the subsystem.

The data type and values of the signal can be:

  • Logical (Boolean) — true (1) or false (0) .

  • Numerical — true (any positive or negative number) or false (0).

Data Types: single | double | int8 | int16 | int32 | uint8 | uint16 | uint32 | Boolean | fixed point

Signal with the initial logical condition. At the beginning of each time step:

  • If IC is false (0), the subsystem does not execute during the time step.

  • If IC is true (value not equal to 0), the subsystem starts executing and continues to repeat execution as long as the cond signal is true.

Data Types: single | double | int8 | int16 | int32 | uint8 | uint16 | uint32 | Boolean | fixed point

Output Arguments

expand all

Signal with the number of While Iterator Subsystem block executions during each time step.

Data Types: double | int8 | int16 | int32

Parameters

expand all

Specify maximum number of iterations allowed during a time step.

-1

Any number of iterations as long as the cond signal is true (value not equal to 0) . If you specify -1 and the cond signal never becomes false (0), the simulation runs in an infinite loop. In this case, the only way to stop the simulation is to terminate MATLAB®.

integer

Maximum number of iterations during a time step.

Programmatic Use

Block parameter: MaxIters
Type: character vector
Value: '5' | '-1' | '<integer>'
Default: '5'

Select type of block.

while

The While Iterator block has two inputs, a cond (logical condition) input and an IC (initial logical condition) input. The source of the IC signal must be external to the While Iterator Subsystem block.

At the beginning of each time step:

  • If the IC input is true (value not equal to 0), the blocks in the subsystem repeat execution while the cond input is true. This process continues during a time step as long as the cond input is true and the number of iterations is less than or equal to the Maximum number of iterations.

  • If the IC input is false, the While Iterator block does not execute the contents of the subsystem.

do-while

The While Iterator block has one input, the cond (while condition) input.

At each time step, the blocks in the subsystem repeat execution while the cond input is true (value not equal to 0). This process continues as long as the cond input is true and the number of iterations is less than or equal to the Maximum number of iterations.

Programmatic Use

Block parameter: WhileBlockType
Type: character vector
Value: 'while' | 'do-while'
Default: 'while'

Select how to handle block states between time steps.

held

Hold block states between time steps. Block state values persist across time steps.

reset

Reset block states to their initial values at the beginning of each time step (in other words, before executing the first iteration loop in the current time step).

Programmatic Use

Block parameter: ResetStates
Type: character vector
Value: 'held' | 'reset'
Default: 'held'

Control display of output port for signal with number of block executions. The value of the signal from this port starts at 1and is incremented by 1 for each succeeding iteration.

off

Remove output port.

on

Display output port for signal with iteration number.

Dependency

Selecting this parameter enables the Output data type parameter.

Programmatic Use

Block parameter: ShowIterationPort
Type: character vector
Value: 'off' | 'on'
Default: 'off'

Select output data type for iteration number signal. The value of this signal is the number of iterations during a time step and the total number of iterations at the end of a time step.

int32

Signed 32-bit integer.

int16

Signed 16-bit integer.

int8

Signed 8-bit integer.

double

Double-precision floating point.

Dependency

Select the Show iteration number port check box to enable this parameter.

Programmatic Use

Block parameter: OutputDataType
Type: character vector
Value: 'int32' | 'int16' | 'int8' | 'double'
Default: 'int32'

Tips

The While Iterator block can output the current iteration number, starting at 1. This model computes N, where N is the first N integers whose sum is less than 100.

The While Iterator subsystem includes these blocks.

The While Iterator block uses these parameter settings:

  • Maximum number of iterations set to 20

  • States when starting set to reset

The model is the diagrammatic equivalent of this pseudocode.

max_sum = 100;
sum = 0;
iteration_number = 0;
cond = (max_sum > 0);
while (cond != 0) {
	iteration_number = iteration_number + 1;
	sum = sum + iteration_number;
	if (sum > max_sum OR iteration_number > max_iterations) 
		cond = 0;
}

Introduced before R2006a

Was this topic helpful?