Documentation Center

  • Trial Software
  • Product Updates

Switch Case

Implement C-like switch control flow statement


Ports & Subsystems


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:

switch (u1) {
	case [u1=1]:
	case [u1=2 or u1=3]:

To construct the Simulink switch control flow statement shown in the above example:

  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 Switch Case block dialog box and update parameters:

    1. Populate the Case conditions field with the individual cases.

    2. To show a default case, select the Show default case check box.

  3. Create a Switch Case Action Subsystem for each case port you added to the Switch Case block.

    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.

  4. Connect each case output port and the default output port of the Switch Case block to the Action port of an Action subsystem.

    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.

  5. In each Switch Case Action Subsystem, enter the Simulink logic appropriate to the case it handles. All blocks in a Switch Case Action Subsystem must run at the same rate as the driving Switch Case block. You can achieve this by setting each block's sample time parameter to be either inherited (-1) or the same value as the Switch Case block's sample time.

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.

  • A scalar value of any enumerated data type, as described in Enumerated Types in Switch Case Blocks.

For more information, see Data Types Supported by Simulink in the Simulink documentation.

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.

Enable zero-crossing detection

Select to enable zero-crossing detection. For more information, see Zero-Crossing Detection.

Sample time (-1 for inherited)

Specify the time interval between samples. To inherit the sample time, set this parameter to -1. See Specify Sample Time for more information.


Direct Feedthrough


Sample Time

Inherited from driving block

Scalar Expansion




Zero-Crossing Detection

Yes, if enabled

Was this topic helpful?