Switch Case

Implement C-like switch control flow
statement

Library

Ports & Subsystems

Description

A Switch Case block receives a single input. Each output port
is attached to aSwitch Case Action Subsystem.
Data outputs are action signals to Switch Case Action subsystems,
which you create with Action Port blocks
and subsystems.

The Switch Case block uses its input value to select a case
condition that determines which subsystem to execute. The cases are
evaluated top down starting with the first case. If a case value (in
brackets) corresponds to the value of the input, its Switch Case Action Subsystem is executed.

If a default case exists, it executes if
none of the other cases executes. Providing a default case
is optional, even if the other case conditions do not exhaust every
possible value. The following diagram shows a completed Simulink®switch control
flow statement:

Cases for the Switch Case block contain an implied break after
their Switch Case Action subsystems are executed. Thus there is no
fall-through behavior for the Simulink switch control
flow statement as found in standard C switch statements.
The following pseudocode represents generated code for the preceding switch control
example:

Enumerated Types in Switch Case Blocks

The Switch Case block supports
enumerated data types for the input signal and the case conditions.
You specify enumerated case conditions as a cell array of enumerated
values in the Case conditions field, for example:

{BasicColors.Red, [BasicColors.Yellow, BasicColors.Blue]}

The Switch Case block icon shows the enumerated names that correspond
to the case conditions, for example:

You can use the enumeration function
to specify a case condition that includes a case for every value in
an enumerated type. When you use enumerated data in a Switch Case
block, follow these rules:

If the input u1 is of an enumerated
type, all case condition values must be of that same enumerated type.

If any case condition value is of an enumerated type,
the input u1 and all other case
condition values must be of that same enumerated type.

When the case condition values are of an enumerated
type, each value that appears as a case condition must have a different
underlying integer.

For more information about enumerated data types in Simulink,
see Use Enumerated Data in Simulink Models.
The generated code for a Switch Case block that uses enumerated data
uses the enumerated values of the specified case conditions, rather
than their underlying integers.

Data Type Support

The input to the port labeled u1 of a Switch
Case block can be:

A scalar value having a built-in data type that Simulink supports.
The block does not support Boolean or fixed-point data types and truncates
the numeric inputs to 32-bit signed integers.

Parameters and Dialog Box

Case conditions

Specify the case conditions using MATLAB® cell notation.
For 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 integer 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 (...).

You can use the enumeration function
to specify a case condition that includes a case for every value in
an enumerated type.

Show
default case

If you select this check box, the default output port appears
as the last case on the Switch Case block, allowing you to specify
a default case. This case executes when the input value does not match
any of the case values specified in the Case conditions field.
With Show default case selected, a default output
port always appears, even if the preceding cases exhaust all possibilities
for the input value.