Using Simulink Previous page   Next Page

Modeling with Control Flow Blocks

The control flow blocks are used to implement the logic of the following C-like control flow statements in Simulink:

Although all the preceding control flow statements are implementable in Stateflow®, these blocks are intended to provide Simulink users with tools that meet their needs for simpler logical requirements.

Creating Conditional Control Flow Statements

You create C-like conditional control flow statements using ordinary subsystems and the following blocks from the Subsystems library.

C Statement
Blocks Used
if-else
If, Action Port
switch
Switch Case, Action Port

If-Else Control Flow Statements

The following diagram depicts a generalized if-else control flow statement implementation in Simulink.

Construct a Simulink if-else control flow statement as follows:

For more detailed information, see the reference topics for the If and Action Port blocks.

Switch Control Flow Statements

The following diagram depicts a generalized switch control flow statement implementation in Simulink.

Construct a Simulink switch control flow statement as follows:

For more detailed information, see the reference topics for the Switch Case and Action Port blocks.

Creating Iterator Control Flow Statements

You create C-like iterator control flow statements using subsystems and the following blocks from the Subsystems library.

C Statement
Blocks Used
do-while
While Iterator
for
For Iterator
while
While Iterator

While Control Flow Statements

The following diagram depicts a generalized C-like while control flow statement implementation in Simulink.

In a Simulink while control flow statement, the While Iterator block iterates the contents of a While subsystem, an atomic subsystem. For each iteration of the While Iterator block, the block programming of the While subsystem executes one complete path through its blocks.

Construct a Simulink while control flow statement as follows:

For specific details, see the reference topic for the While Iterator block.

For Control Flow Statements

The following diagram depicts a generalized for control flow statement implementation in Simulink.

In a Simulink for control flow statement, the For Iterator block iterates the contents of a For Iterator Subsystem, an atomic subsystem. For each iteration of the For Iterator block, the block programming of the For Iterator Subsystem executes one complete path through its blocks.

Construct a Simulink for control flow statement as follows:

The For Iterator block works well with the Assignment block to reassign values in a vector or matrix. This is demonstrated in the following example. Note the matrix dimensions in the data being passed.

The above example outputs the sin value of an input 2-by-5 matrix (2 rows, 5 columns) using a For subsystem containing an Assignment block. The process is as follows:

  1. A 2-by-5 matrix is input to the Selector block and the Assignment block.
  2. The Selector block strips off a 2-by-1 matrix from the input matrix at the column value indicated by the current iteration value of the For Iterator block.
  3. The sine of the 2-by-1 matrix is taken.
  4. The sine value 2-by-1 matrix is passed to an Assignment block.
  5. The Assignment block, which takes the original 2-by-5 matrix as one of its inputs, assigns the 2-by-1 matrix back into the original matrix at the column location indicated by the iteration value.
  1. The rows specified for reassignment in the property dialog for the Assignment block in the above example are [1,2]. Because there are only two rows in the original matrix, you could also have specified -1 for the rows, i.e., all rows.


Previous page  Building Simulation Targets Comparing Stateflow and Control Flow Statements Next page

© 1994-2005 The MathWorks, Inc.