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 a Switch 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.

Control Algorithm Execution Using Enumerated Signal

This example shows how to use a signal of an enumerated data type to control the execution of a block algorithm. For basic information about using enumerated data types in models, see Use Enumerated Data in Simulink Models.

When you use enumerated data in a Switch Case block, follow these best practices:

  • Use the same enumerated type for the input u1 and all of the case condition values.

  • Use a different underlying integer for each of the enumerated values that you specify in the Case conditions box.

Define Enumerated Type

Copy the enumerated type definition ex_SwitchCase_MyColors into a script file in your current folder.

classdef ex_SwitchCase_MyColors < Simulink.IntEnumType

Alternatively, you can use the function Simulink.defineIntEnumType to define the type.


Explore Example Model

  1. Open the example model ex_enum_switch_case.

  2. Open the Enumerated Constant block dialog box. The constant output value is ex_SwitchCase_MyColors.Blue.

  3. Open the Switch Case block dialog box. The Case conditions box is set to a cell array containing three of the four possible enumeration members. The block has four outputs corresponding to the three specified enumeration members and a default case.

  4. Open the Switch Case Action Subsystem blocks. The subsystems each contain a Constant block that uses a unique constant value.

Control Execution During Simulation

  1. In the Simulink Editor, set the simulation stop time to Inf.

  2. Simulate the model. The Display block shows the value 5, which corresponds to the case ex_SwitchCase_MyColors.Blue.

  3. In the Enumerated Constant block dialog box, set Value to ex_SwitchCase_MyColors.Red and click Apply. The Display block shows 19.

  4. Set Value to ex_SwitchCase_MyColors.Mauve and click Apply. The Display block shows 3, which corresponds to the default case.

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 Control Algorithm Execution Using Enumerated Signal.

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


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


This parameter is not visible in the block dialog box unless it is explicitly set to a value other than -1. To learn more, see Blocks for Which Sample Time Is Not Recommended.


Data Types

Double | Single | Base Integer | Enumerated

Sample Time

Inherited from driving block

Direct Feedthrough


Multidimensional Signals


Variable-Size Signals


Zero-Crossing Detection

Yes, if enabled

Code Generation


Introduced before R2006a

Was this topic helpful?