Using Simulink    

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.

Comparing Stateflow and Control Flow Statements

Stateflow already possesses the logical capabilities of the Simulink control flow statements. It can call Function-Call subsystems (see Function-Call Subsystems) on condition or iteratively. However, since Stateflow provides a great deal more in logical sophistication, if your requirements are simpler, you might find the capabilities of the Simulink control flow blocks sufficient for your needs. In addition, the control flow statements offer a few advantages, which are listed in the following topics.

Sample Times.   The Function-Call subsystems that Stateflow can call are triggered subsystems. Triggered subsystems inherit their sample times from the calling block. However, the Action subsystems used in if-else and switch control flow statements and the While and For subsystems that make up while and for control flow statements are enabled subsystems. Enabled subsystems can have their own sample times independent of the calling block. This also allows you to use more categories of blocks in your iterated subsystem than in a Function-Call subsystem.

Resetting of States When Reenabled.   Simulink control flow statement blocks allow you to retain or reset (to their initial values) the values of states for Action, For, and While subsystems when they are reenabled. For detailed information, see the references for the While Iterator and For Iterator blocks regarding the parameter States when starting and the reference for the Action Port block regarding the parameter States when execution is resumed.

Using Stateflow with the Control Flow Blocks

You might want to consider the possibility of using Stateflow and the Simulink control flow blocks together. The following sections contain some examples that give you a few suggestions on how to combine the two.

Using Stateflow with If-Else or Switch Subsystems.   In the following model, Stateflow places one of a variety of values in a Stateflow data object. Upon chart termination, a Simulink if control flow statement uses that data to make a conditional decision.

.

In this case, control is given to a Switch Case block, which uses the value to choose one of several case subsystems to execute.

Using Stateflow with While Subsystems.   In the following diagram, Stateflow computes the value of a data object that is available to a condition input of a While Iterator block in do-while mode.

The While Iterator block has iterative control over its host subsystem, which includes the Stateflow Chart block. In do-while mode, the While block is guaranteed to operate for its first iteration value ( = 1 ). During that time, the Stateflow chart is awakened and sets a data value used by the While Iterator block, which is evaluated as a condition for the next while iteration.

In the following diagram, the While block is now set in while mode. In this mode, the While Iterator block must have input to its initial condition port in order to execute its first iteration value. This value must come from outside the While subsystem.

If the initial condition is true, the While Iterator block wakes up the Stateflow chart and executes it to termination. During that time the Stateflow chart sets data, which the While Iterator condition port uses as a condition for the next iteration.


  Triggered and Enabled Subsystems Model Discretizer