Main Content

Switch Case

Select subsystem execution using logic similar to a switch statement

  • Library:
  • Simulink / Ports & Subsystems

  • Switch Case block

Description

The Switch Case block with Switch Case Action Subsystem blocks containing Action Port blocks, implements switch logic to control subsystem execution.

A Switch Case block has a single input. To select a case, define the input value using the Case conditions parameter. The cases are evaluated top down starting with the first case.

Each case is associated with an output port that is attached to a Switch Case Action Subsystem block. When a case is selected, the associated output port sends an action signal to execute the subsystem.

A default case is selected after all of the other case conditions evaluate to false. Providing a default case is optional, even if the other case conditions do not exhaust every possible input value.

Cases for the Switch Case block contain an implied break after a Switch Case Action Subsystem block is executed. Therefore, there is no fall through behavior for the Simulink® Switch Case block as found in standard C switch statements.

Ports

Input

expand all

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

  • A scalar value with a built-in data type that Simulink supports. However, the Switch Case block does not support Boolean or fixed-point data types, and it truncates numeric inputs to 32-bit signed integers.

  • A scalar value of any enumerated data type.

Data Types: single | double | int8 | int16 | int32 | uint8 | uint16 | uint32 | enumerated

Output

expand all

Output from the Case and default ports are action signals connected to Switch Case Action Subsystem blocks.

Parameters

expand all

Specify the cases values using MATLAB® cell notation.

{1}

Specify the output port labeled case[1]outputs an action signal when the input port value is 1.

list of ports with case assignments

Specify multiple cases and ports 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 from a long list of case conditions are displayed in shortened form on the face of the Switch Case block, using a terminating ellipsis (...).

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

Programmatic Use

Block Parameter: CaseConditions
Type: character vector
Values: '{1}' | '<list of cases>'
Default: '{1}'

Control display of default output port.

off

Hide default output port.

on

Display default output port as the last case on the Switch Case block. This allows you to specify a default case that executes when the input value does not match any of the other case values.

Programmatic Use

Block Parameter: ShowDefaultCase
Type: character vector
Values: 'off' | 'on'
Default: 'off'

Control zero-crossing detection.

on

Detect zero crossings.

off

Do not detect zero crossings.

Programmatic Use

Block Parameter: ZeroCross
Type: character vector
Values: 'on' | 'off'
Default: 'on'

Block Characteristics

Data Types

double | enumerated | integer | single

Direct Feedthrough

yes

Multidimensional Signals

no

Variable-Size Signals

no

Zero-Crossing Detection

yes

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using Simulink® Coder™.

Introduced before R2006a