Quantcast

Documentation Center

  • Trial Software
  • Product Updates

Switch

Switch output between first input and third input based on value of second input

Library

Signal Routing

Description

Types of Block Inputs

The Switch block passes through the first input or the third input based on the value of the second input. The first and third inputs are called data inputs. The second input is called the control input. Specify the condition under which the block passes the first input by using the Criteria for passing first input and Threshold parameters.

To immediately back propagate a known output data type to the first and third input ports, set the Output data type parameter to Inherit: Inherit via internal rule and select the Require all data port inputs to have the same data type check box.

Limitations on Data Inputs

The sizes of the two data inputs can be different if you select Allow different data input sizes on the block dialog box. However, this block does not support variable-size input signals. Therefore, the size of each input cannot change during simulation.

If the data inputs to the Switch block are buses, the element names of both buses must be the same. Using the same element names ensures that the output bus has the same element names no matter which input bus the block selects. To ensure that your model meets this requirement, use a bus object to define the buses and set the Element name mismatch diagnostic to error. See Connectivity Diagnostics Overview for more information.

Block Icon Appearance

The block icon helps you identify Criteria for passing first input and Threshold without having to open the block dialog box.

For information about port order for various block orientations, see How to Rotate a Block in the Simulink® documentation.

Block Behavior for Boolean Control Input

When the control input is a Boolean signal, use one of these combinations of criteria and threshold value:

  • u2 >= Threshold, where the threshold value equals 1

  • u2 > Threshold, where the threshold value equals 0

  • u2 ~=0

Otherwise, the Switch block ignores the threshold and uses the Boolean input for signal routing. For a control input of 1, the block passes the first input, and for a control input of 0, the block passes the third input. In this case, the block icon changes after compile time and uses T and F to label the first and third inputs, respectively.

Data Type Support

The control input can be of any data type that Simulink supports, including fixed-point and enumerated types. The control input cannot be complex. If the control input is enumerated, the Threshold parameter must be a value of the same enumerated type.

The data inputs can be of any data type that Simulink supports. If either data input is of an enumerated type, the other must be of the same enumerated type.

When the output is of enumerated type, both data inputs should use the same enumerated type as the output.

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

Parameters and Dialog Box

The Switch block dialog box appears as follows:

Criteria for passing first input

Select the condition under which the block passes the first input. If the control input meets the condition set in the Criteria for passing first input parameter, the block passes the first input. Otherwise, the block passes the third input.

Settings

Default: u2 > Threshold

u2 >= Threshold

Checks whether the control input is greater than or equal to the threshold value.

u2 > Threshold

Checks whether the control input is greater than the threshold value.

u2 ~=0

Checks whether the control input is nonzero.

    Note:   The Switch block does not support u2 ~=0 mode for enumerated data types.

Tip

When the control input is a Boolean signal, use one of these combinations of condition and threshold value:

  • u2 >= Threshold, where the threshold value equals 1

  • u2 > Threshold, where the threshold value equals 0

  • u2 ~=0

Otherwise, the Switch block ignores threshold values and uses the Boolean value for signal routing. For a value of 1, the block passes the first input, and for a value of 0, the block passes the third input. A warning message that describes this behavior also appears in the MATLAB® Command Window.

Dependencies

Selecting u2 ~=0 disables the Threshold parameter.

Command-Line Information

See Block-Specific Parameters for the command-line information.

Threshold

Assign the switch threshold that determines which input the block passes to the output.

Settings

Default: 0

Minimum: value from the Output minimum parameter

Maximum: value from the Output maximum parameter

Tip

To specify a nonscalar threshold, use brackets. For example, the following entries are valid:

  • [1 4 8 12]

  • [MyColors.Red, MyColors.Blue]

Dependencies

Setting Criteria for passing first input to u2 ~=0 disables this parameter.

Command-Line Information

See Block-Specific Parameters for the command-line information.

Enable zero-crossing detection

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

Settings

Default: On

On

Enable zero-crossing detection.

Off

Do not enable zero-crossing detection.

Command-Line Information

Parameter: ZeroCross
Type: string
Value: 'on' | 'off'
Default: 'on'

Sample time (-1 for inherited)

Enter the discrete interval between sample time hits or specify another appropriate sample time such as continuous or inherited.

Settings

Default: -1

By default, the block inherits its sample time based upon the context of the block within the model. To set a different sample time, enter a valid sample time based upon the table in Types of Sample Time.

See also Specify Sample Time in the online documentation for more information.

Command-Line Information

See Block-Specific Parameters for the command-line information.

Require all data port inputs to have the same data type

Require all data inputs to have the same data type.

Settings

Default: Off

On

Requires all data inputs to have the same data type.

Off

Does not require all data inputs to have the same data type.

Command-Line Information

See Block-Specific Parameters for the command-line information.

Lock output data type setting against changes by the fixed-point tools

Select to lock the output data type setting of this block against changes by the Fixed-Point Tool and the Fixed-Point Advisor.

Settings

Default: Off

On

Locks the output data type setting for this block.

Off

Allows the Fixed-Point Tool and the Fixed-Point Advisor to change the output data type setting for this block.

Command-Line Information

Parameter: LockScale
Type: string
Value: 'off' | 'on'
Default: 'off'

See Also

For more information, see Use Lock Output Data Type Setting.

Integer rounding mode

Specify the rounding mode for fixed-point operations.

Settings

Default: Floor

Ceiling

Rounds both positive and negative numbers toward positive infinity. Equivalent to the MATLAB ceil function.

Convergent

Rounds number to the nearest representable value. If a tie occurs, rounds to the nearest even integer. Equivalent to the Fixed-Point Designer™ convergent function.

Floor

Rounds both positive and negative numbers toward negative infinity. Equivalent to the MATLAB floor function.

Nearest

Rounds number to the nearest representable value. If a tie occurs, rounds toward positive infinity. Equivalent to the Fixed-Point Designer nearest function.

Round

Rounds number to the nearest representable value. If a tie occurs, rounds positive numbers toward positive infinity and rounds negative numbers toward negative infinity. Equivalent to the Fixed-Point Designer round function.

Simplest

Automatically chooses between round toward floor and round toward zero to generate rounding code that is as efficient as possible.

Zero

Rounds number toward zero. Equivalent to the MATLAB fix function.

Command-Line Information

Parameter: RndMeth
Type: string
Value: 'Ceiling' | 'Convergent' | 'Floor' | 'Nearest' | 'Round' | 'Simplest' | 'Zero'
Default: 'Floor'

See Also

For more information, see Rounding in the Fixed-Point Designer documentation.

Saturate on integer overflow

Specify whether overflows saturate.

Settings

Default: Off

On

Overflows saturate to either the minimum or maximum value that the data type can represent.

For example, an overflow associated with a signed 8-bit integer can saturate to -128 or 127.

Off

Overflows wrap to the appropriate value that is representable by the data type.

For example, the number 130 does not fit in a signed 8-bit integer and wraps to -126.

Tips

  • Consider selecting this check box when your model has possible overflow and you want explicit saturation protection in the generated code.

  • Consider clearing this check box when you want to optimize efficiency of your generated code.

    Clearing this check box also helps you avoid overspecifying how a block handles out-of-range signals. For more information, see Checking for Signal Range Errors.

  • When you select this check box, saturation applies to every internal operation on the block, not just the output or result.

  • In general, the code generation process can detect when overflow is not possible. In this case, the code generator does not produce saturation code.

Command-Line Information

Parameter: SaturateOnIntegerOverflow
Type: string
Value: 'off' | 'on'
Default: 'off'

Allow different data input sizes

Select this check box to allow input signals with different sizes.

Settings

Default: Off

On

Allows input signals with different sizes, and propagates the input signal size to the output signal. If the two data inputs are variable-size signals, the maximum size of the signals can be equal or different.

Off

Inputs signals must be the same size.

Command-Line Information

Parameter: AllowDiffInputSize
Type: string
Value: 'on' | 'off'
Default: 'off'

Output minimum

Specify the minimum value that the block should output.

Settings

Default: []

The default value is [] (unspecified).

Simulink uses this value to perform:

Tip

This number must be a finite real double scalar value.

Command-Line Information

See Block-Specific Parameters for the command-line information.

Output maximum

Specify the maximum value that the block should output.

Settings

Default: []

The default value is [] (unspecified).

Simulink uses this value to perform:

Tip

This number must be a finite real double scalar value.

Command-Line Information

See Block-Specific Parameters for the command-line information.

Output data type

Specify the output data type.

Settings

Default: Inherit: Inherit via internal rule

Inherit: Inherit via internal rule

Uses the following rules to determine the output data type.

Data Type of First Input PortOutput Data Type
Has a larger positive range than the third input portInherited from the first input port
Has the same positive range as the third input portInherited from the third input port
Has a smaller positive range than the third input port

Inherit: Inherit via back propagation

Uses data type of the driving block.

double

Specifies output data type is double.

single

Specifies output data type is single.

int8

Specifies output data type is int8.

uint8

Specifies output data type is uint8.

int16

Specifies output data type is int16.

uint16

Specifies output data type is uint16.

int32

Specifies output data type is int32.

uint32

Specifies output data type is uint32.

fixdt(1,16,0)

Specifies output data type is fixed point fixdt(1,16,0).

fixdt(1,16,2^0,0)

Specifies output data type is fixed point fixdt(1,16,2^0,0).

Enum: <class name>

Uses an enumerated data type, for example, Enum: BasicColors.

<data type expression>

Uses a data type object, for example, Simulink.NumericType.

Tip

When the output is of enumerated type, both data inputs should use the same enumerated type as the output.

Command-Line Information

See Block-Specific Parameters for the command-line information.

See Also

See Specify Block Output Data Types for more information.

Mode

Select the category of data to specify.

Settings

Default: Inherit

Inherit

Specifies inheritance rules for data types. Selecting Inherit enables a list of possible values:

  • Inherit via internal rule (default)

  • Inherit via back propagation

Built in

Specifies built-in data types. Selecting Built in enables a list of possible values:

  • double (default)

  • single

  • int8

  • uint8

  • int16

  • uint16

  • int32

  • uint32

Fixed point

Specifies fixed-point data types.

Enumerated

Specifies enumerated data types. Selecting Enumerated enables you to enter a class name.

Expression

Specifies expressions that evaluate to data types. Selecting Expression enables you to enter an expression.

Dependencies

Clicking the Show data type assistant button enables this parameter.

Command-Line Information

See Block-Specific Parameters for the command-line information.

See Also

See Specify Data Types Using Data Type Assistant.

Data type override

Specify data type override mode for this signal.

Settings

Default: Inherit

Inherit

Inherits the data type override setting from its context, that is, from the block, Simulink.Signal object or Stateflow® chart in Simulink that is using the signal.

Off

Ignores the data type override setting of its context and uses the fixed-point data type specified for the signal.

Tip

The ability to turn off data type override for an individual data type provides greater control over the data types in your model when you apply data type override. For example, you can use this option to ensure that data types meet the requirements of downstream blocks regardless of the data type override setting.

Dependency

This parameter appears only when the Mode is Built in or Fixed point.

Signedness

Specify whether you want the fixed-point data as signed or unsigned.

Settings

Default: Signed

Signed

Specifies the fixed-point data as signed.

Unsigned

Specifies the fixed-point data as unsigned.

Dependencies

Selecting Mode > Fixed point enables this parameter.

Command-Line Information

See Block-Specific Parameters for the command-line information.

See Also

See Specifying a Fixed-Point Data Type.

Word length

Specify the bit size of the word that holds the quantized integer.

Settings

Default: 16

Minimum: 0

Maximum: 32

Large word sizes represent large values with greater precision than small word sizes.

Dependencies

Selecting Mode > Fixed point enables this parameter.

Command-Line Information

See Block-Specific Parameters for the command-line information.

See Also

See Specifying a Fixed-Point Data Type for more information.

Scaling

Specify the method for scaling your fixed-point data to avoid overflow conditions and minimize quantization errors.

Settings

Default: Best precision

Binary point

Specify binary point location.

Slope and bias

Enter slope and bias.

Best precision

Specify best-precision values.

Dependencies

Selecting Mode > Fixed point enables this parameter.

Selecting Binary point enables:

  • Fraction length

  • Calculate Best-Precision Scaling

Selecting Slope and bias enables:

  • Slope

  • Bias

  • Calculate Best-Precision Scaling

See Also

For more information, see Specifying a Fixed-Point Data Type.

Fraction length

Specify fraction length for fixed-point data type.

Settings

Default: 0

Binary points can be positive or negative integers.

Dependencies

Selecting Scaling > Binary point enables this parameter.

Command-Line Information

See Block-Specific Parameters for the command-line information.

See Also

See Specifying a Fixed-Point Data Type for more information.

Slope

Specify slope for the fixed-point data type.

Settings

Default: 2^0

Specify any positive real number.

Dependencies

Selecting Scaling > Slope and bias enables this parameter.

Command-Line Information

See Block-Specific Parameters for the command-line information.

See Also

See Specifying a Fixed-Point Data Type for more information.

Bias

Specify bias for the fixed-point data type.

Settings

Default: 0

Specify any real number.

Dependencies

Selecting Scaling > Slope and bias enables this parameter.

Command-Line Information

See Block-Specific Parameters for the command-line information.

See Also

See Specifying a Fixed-Point Data Type for more information.

Bus Support

The Switch block is a bus-capable block. The data inputs can be virtual or nonvirtual bus signals subject to the following restrictions:

  • All the buses must be equivalent (same hierarchy with identical names and attributes for all elements).

  • All signals in a nonvirtual bus input to a Switch block must have the same sample time. The requirement holds even if the elements of the associated bus object specify inherited sample times.

You can use a Rate Transition block to change the sample time of an individual signal, or of all signals in a bus. See Composite Signals and Bus-Capable Blocks for more information.

You can use an array of buses as an input signal to a Switch block. For details about defining and using an array of buses, see Combine Buses into an Array of Buses. When using an array of buses with a Switch block, set the Threshold parameter to a scalar value.

Examples

Use of Boolean Input for the Control Port

In the sldemo_fuelsyssldemo_fuelsys model, the fuel_rate_control/airflow_calc subsystem uses a Switch block:

The value of the control port on the Switch block determines whether or not feedback correction occurs. The control port value depends on the output of the Logical Operator block.

When the Logical Operator block output is...The control port of the Switch block is...And feedback control...
TRUE1Occurs
FALSE0Does not occur

Use of Simulation Time for the Control Port

The sldemo_zeroxingsldemo_zeroxing model uses a Switch block:

The value of the control port on the Switch block determines when the output changes from the first input to the third input.

When simulation time is...The Switch block output is...
Less than or equal to 5The first input from the Abs block
Greater than 5The third input from the Saturation block

Characteristics

Bus-capable

Yes, with restrictions

Direct Feedthrough

Yes

Sample Time

Specified in the Sample time parameter

Scalar Expansion

Yes

Dimensionalized

Yes

Multidimensionalized

Yes

Zero-Crossing Detection

Yes, if enabled

Was this topic helpful?