| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Simulink |
| Contents | Index |
| Learn more about Simulink |
Ports & Subsystems
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 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:
switch (u1) {
case [u1=1]:
body_1;
break;
case [u1=2 or u1=3]:
body_23;
break;
default:
bodydefault;
}
You construct a Simulink switch control flow statement like the example shown as follows:
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.
Open the Switch Case block dialog box and update parameters:
Enter the Case conditions field with the individual cases.
Each case can be an integer or set of integers specified with MATLAB cell notation. See the Case conditions field in the "Parameters and Dialog Box" section of this reference.
Select the Show default case check box to show a default case output port on the Switch Case block.
If all other cases are false, the default case is taken.
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.
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.
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.
Note As demonstrated in the preceding pseudocode example, cases for the Switch Case block contain an implied break after their Switch Case Action subsystems are executed. There is no fall-through behavior for the Simulink switch control flow statement as found in standard C switch statements. |
Input to the port labeled u1 of a Switch Case block can be a scalar value of any numeric data type supported by Simulink software, including a fixed-point data type. The input to u1 cannot be Boolean or have an enumerated type. Noninteger inputs are truncated. Data outputs are action signals to Switch Case Action subsystems that you create with Action Port blocks and subsystems.

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 (...).
If you select this check box, 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.
Select to enable zero-crossing detection. For more information, see Zero-Crossing Detection.
Specify the time interval between samples. To inherit the sample time, set this parameter to -1. See How to Specify the Sample Time in the online documentation for more information.
Direct Feedthrough | Yes |
Sample Time | Inherited from driving block |
Scalar Expansion | No |
Dimensionalized | No |
Zero-Crossing Detection | Yes, if enabled |
![]() | Switch | Switch Case Action Subsystem | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |