Products & Services Solutions Academia Support User Community Company

Learn more about Simulink   

Modeling Control Flow Logic

Equivalent C Language Statements

You can use block diagrams to model control flow logic equivalent to the following C programming language statements:

Modeling Conditional Control Flow Logic

You can use the following blocks to model conditional control flow logic.

C StatementEquivalent Blocks

if-else

If, If Action Subsystem

switch

Switch Case, Switch Case Action Subsystem

Modeling If-Else Control Flow

The following diagram models if-else control flow.

Construct an if-else control flow diagram as follows:

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

Modeling Switch Control Flow

The following diagram models switch control flow.

Construct a switch control flow statement as follows:

For more detailed information, see Simulink Reference for the Switch Case and Action Port blocks.

Modeling While and For Loops

The following blocks allow you to model while and for loops.

C StatementEquivalent Blocks

do-while

While Iterator Subsystem

for

For Iterator Subsystem

while

While Iterator Subsystem

Modeling While Loops

The following diagram illustrates a while loop.

In this example, the Simulink software repeatedly executes the contents of the While subsystem at each time step until a condition specified by the While Iterator block is satisfied. In particular, for each iteration of the loop specified by the While Iterator block, the Simulink software invokes the update and output methods of all the blocks in the While subsystem in the same order that the methods would be invoked if they were in a noniterated atomic subsystem.

Construct a while loop as follows:

For more information, see the While Iterator block.

Modeling For Loops

The following diagram models a for loop:

In this example, the Simulink software executes the contents of the For subsystem multiples times at each time step with the number of iterations being specified by the input to the For Iterator block. In particular, for each iteration of the for loop, the Simulink software invokes the update and output methods of all the blocks in the For subsystem in the same order that the methods would be invoked if they were in a noniterated atomic subsystem.

Construct a for loop 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 sine 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.

    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.

      Note   Experienced Simulink software users will note that the Trigonometric Function block is already capable of taking the sine of a matrix. The above example uses the Trigonometric Function block only as an example of changing each element of a matrix with the collaboration of an Assignment block and a For Iterator block.

  


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