Simulink Reference    
Switch Case

Implement a C-like switch control flow statement

Library

Ports & Subsystems

Description


The following shows a completed Simulink C-like switch control flow statement in the subsystem of the Switch Case block.

A Switch Case block receives a single input, which it uses to form case conditions that determine which the subsystem to execute. Each output port case condition is attached to a Switch Case Action subsystem. The cases are evaluated top down starting with the top case. If a case value (in brackets) corresponds to the actual value of the input, its Switch Case Action subsystem is executed.

The preceding switch control flow statement can be represented by the following pseudocode:

You construct a Simulink switch control flow statement like the example shown as follows:

  1. Place a Switch Case block in the current system and attach the input port labeled u1 to the source of the data you are evaluating.
  2. Open the Block Parameters dialog of the Switch Case block and enter as follows:
    1. Enter the Case conditions field with the individual cases.
    1. Select the Show default case check box to show a default case output port on the Switch Case block.
  3. Create a Switch Case Action subsystem for each case port you added to the Switch Case block.
  1. These consist of subsystems with Action Port blocks inside them. When you place the Action Port block inside a subsystem, the subsystem becomes an atomic subsystem with an input port labeled Action.

  1. Connect each case output port and the default output port of the Switch Case block to the Action port of an Action subsystem.
  1. Each connected subsystem becomes a case body. This is indicated by the change in label for the Switch Case Action subsystem block and the Action Port block inside of it to the name case{}.

    During simulation of a switch control flow statement, the Action signals from the Switch Case block to each Switch Case Action subsystem turn from solid to dashed.

  1. In each Switch Case Action subsystem, enter the Simulink logic appropriate to the case it handles.

Data Type Support

Input to the port labeled u1 of a Switch Case block can be a scalar value of any data type, including fixed-point data types, except boolean, int64, and uint64. Noninteger inputs are truncated.

Data outputs are action signals to Switch Case Action subsystems that are created with Action Port blocks and subsystems.

Parameters and Dialog Box

Case conditions
Case conditions are specified using MATLAB cell notation where each cell is a case condition consisting of integers or arrays of integers. In the preceding dialog example, entering {1,[7,9,4]} specifies that output port case[1] is run when the input value is 1, and output port case[7 9 4] is run when the input value is 7, 9, or 4.
You can use colon notation to specify a range of case conditions. For example, entering {[1:5]} specifies that output port case[1 2 3 4 5] is run when the input value is 1, 2, 3, 4, or 5.
Depending on block size, cases with long lists of conditions are displayed in shortened form in the Switch Case block, using a terminating ellipsis (...).
Show default case
If this check box is selected, the default output port appears as the last case on the Switch Case block. This case is run when the input value does not match any of the case values specified in the Case conditions field.
Enable zero crossing detection
Select to enable use of zero crossing detection. For more information, see Zero Crossing Detection in the Using Simulink documentation.

Characteristics

Direct Feedthrough
Yes
Sample Time
Inherited from driving block
Scalar Expansion
No
Dimensionalized
No
Zero Crossing
Yes, if zero-crossing detection is enabled.


  Switch Switch Case Action Subsystem