Documentation Center

  • Trial Software
  • Product Updates

Multiport Switch

Choose between multiple block inputs

Library

Signal Routing

Description

Types of Block Inputs

The Multiport Switch block chooses among several inputs. The first input is the control input, while the others are data inputs. The value of the control input determines which data input passes to the output port.

    Note:   This block is the same as the Index Vector block. If you set Number of data ports to 1, you get the Index Vector block.

When you set Data port for default case to Additional data port, an extra data port with a * label appears below the other data inputs. For more information, see How the Block Handles an Out-of-Range Control Input.

Rules That Determine the Block Output

You specify the number of data inputs with Number of data ports. Data inputs can be scalar or vector.

  • If you specify only one data input and that input is a vector, the block behaves as an index selector, and not as a multiport switch. The block output is the input vector element whose index matches the control input.

  • If you specify more than one data input, the block behaves as a multiport switch. The block output is the data input that corresponds to the value of the control input. If at least one of the data inputs is a vector, the block output is a vector. In this case, the block expands any scalar inputs to vectors.

  • If the data inputs are scalar, the output is a scalar.

How the Block Interprets the Control Input

The following table summarizes how the block interprets the control input and passes data to the output.

Control InputTruncationSetting for Data Port OrderBlock Behavior During Simulation
The specified data input passes to the output based on...And an out-of-range error occurs when...

Integer value

None

Zero-based contiguous

Zero-based indexing

The control input is less than 0 or greater than the number of data inputs minus one

One-based contiguous

One-based indexing

The control input is less than 1 or greater than the number of data inputs

Specify indices

Indices you specify

The control input does not correspond to any specified data port index

Not an integer value

The block truncates the value to an integer by rounding to floor.

Zero-based contiguous

Zero-based indexing

The truncated control input is less than 0 or greater than the number of data inputs minus one

One-based contiguous

One-based indexing

The truncated control input is less than 1 or greater than the number of data inputs

Specify indices

Indices you specify

The truncated control input does not correspond to any specified data port index

How the Block Handles an Out-of-Range Control Input

For a control input with an integer value, the input is out of range when the value does not match any data port indices. For a control input that is not an integer value, the input is out of range when the truncated value does not match any data port indices. In both cases, the block behavior depends on your settings for Data port for default case and Diagnostic for default case.

Behavior for Simulation

The following behavior applies only to simulation for your model.

Data port for default caseDiagnostic for default case
NoneWarningError

Last data port

Use the last data port and do not report any warning or error.

Use the last data port and report a warning.

Report an error and stop simulation.

Additional data port

Use the additional data port with a * label and do not report any warning or error.

Use the additional data port with a * label and report a warning.

Report an error and stop simulation.

Behavior for Code Generation

The following behavior applies to code generation for your model.

Data port for default caseDiagnostic for default case
NoneWarningError

Last data port

Use the last data port.

Use the last data port.

Use the last data port.

Additional data port

Use the additional data port with a * label.

Use the additional data port with a * label.

Use the additional data port with a * label.

Guidelines on Setting Parameters for Enumerated Control Port

When the control port on the Multiport Switch block is of enumerated type, follow these guidelines:

ScenarioWhat to DoRationale

The enumerated type contains a value that represents invalid, out-of-range, or uninitialized values.

  • Set Data port order to Specify indices.

  • Set Data port indices to use this value for the last data port.

  • Set Data port for default case to Last data port.

This block configuration handles invalid values that the enumerated type explicitly represents.

The enumerated type contains only valid enumerated values. However, a data input port can get invalid values of enumerated type.

  • Set Data port for default case to Additional data port.

This block configuration handles invalid values that the enumerated type does not explicitly represent.

The enumerated type contains only valid enumerated values. Data input ports can never get invalid values of enumerated type.

  • Set Data port for default case to Last data port.

  • Set Diagnostic for default case to None.

This block configuration avoids unnecessary diagnostic action.

The block does not have a data input port for every value of the enumerated type.

  • Set Data port for default case to Additional data port.

This block configuration handles enumerated values that do not have a data input port, along with invalid values.

Data Type Support

The control signal can be of any data type that Simulink® supports, including fixed-point and enumerated types. If the control signal is numeric, it cannot be complex. If the control signal is an enumerated signal, the block uses the value of the underlying integer to select a data port. If the underlying integer does not correspond to a data port, an error occurs.

The data signals can be of any data type that Simulink supports. If any data signal is of an enumerated type, all others must be of the same enumerated type.

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

Parameters and Dialog Box

The Main pane of the Multiport Switch block dialog box appears as follows:

The Signal Attributes pane of the Multiport Switch block dialog box appears as follows:

Data port order

Specify the type of ordering for your data input ports.

Settings

Default: One-based contiguous (for Multiport Switch block), Zero-based contiguous (for Index Vector block)

Zero-based contiguous

Block uses zero-based indexing for ordering contiguous data ports.

One-based contiguous

Block uses one-based indexing for ordering contiguous data ports.

Specify indices

Block uses noncontiguous indexing for ordering data ports.

Tips

  • When the control port is of enumerated type, select Specify indices.

  • If you select Zero-based contiguous or One-based contiguous, verify that the control port is not of enumerated type. This configuration is deprecated and produces an error. You can run slupdate on your model to replace each Multiport Switch block of this configuration with a block that explicitly specifies data port indices.

  • Avoid situations where the block contains unused data ports for simulation or code generation. When the control port is of fixed-point or built-in data type, verify that all data port indices are representable with that type. Otherwise, the following block behavior occurs:

    If the block has unused data ports and data port order is...You get...
    Zero-based contiguous or One-based contiguousA warning
    Specify indicesAn error

Dependencies

Selecting Zero-based contiguous or One-based contiguous enables the Number of data ports parameter.

Selecting Specify indices enables the Data port indices parameter.

Command-Line Information

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

Number of data ports

Specify the number of data input ports to the block.

Settings

Default: 3 (for Multiport Switch block), 1 (for Index Vector block)

The block icon changes to match the number of data input ports you specify.

Dependency

Selecting Zero-based contiguous or One-based contiguous for Data port order enables this parameter.

Command-Line Information

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

Data port indices

Specify an array of indices for your data ports.

Settings

Default: {1,2,3}

The block icon changes to match the data port indices you specify.

Tips

  • To specify an array of indices that correspond to all values of an enumerated type, enter enumeration('type_name') for this parameter. Do not include braces.

    For example, enumeration('MyColors') is a valid entry.

  • To enter specific values of an enumerated type, use the type_name.enumerated_name format. Do not enter the underlying integer value.

    For example, {MyColors.Red, MyColors.Green, MyColors.Blue} is a valid entry.

  • To indicate that more than one value maps to a data port, use brackets.

    For example, the following entries are both valid:

    • {MyColors.Red, MyColors.Green, [MyColors.Blue, MyColors.Yellow]}

    • {[3,5],0,18}

  • If the control port is of fixed-point or built-in data type, the values for Data port indices must be representable with that type. Otherwise, an error appears at compile time to alert you to unused data ports.

  • If the control port is of enumerated data type, the values for Data port indices must be enumerated values of that type.

  • If Data port indices contains values of enumerated type, the control port must be of that data type.

Dependency

Selecting Specify indices for Data port order enables this parameter.

Command-Line Information

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

Data port for default case

Specify whether to use the last data port for out-of-range inputs, or to use an additional port.

Settings

Default: Last data port

Last data port

Block uses the last data port for output when the control port value does not match any data port indices.

Additional data port

Block uses an additional data port for output when the control port value does not match any data port indices.

Tip

If you set this parameter to Additional data port and Number of data ports is 3, the number of input ports on the block is 5. The first input is the control port, the next three inputs are data ports, and the fifth input is the default port for out-of-range inputs.

Command-Line Information

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

Diagnostic for default case

Specify the diagnostic action to take when the control port value does not match any data port indices.

Settings

Default: Error

None

Do not show any warning or error message.

Warning

Show a warning message in the MATLAB® Command Window and continue the simulation.

Error

Stop simulation and display an error in the Diagnostic Viewer. In this case, the Data port for default case is used only for code generation and not simulation.

For more information, see How the Block Handles an Out-of-Range Control Input.

Command-Line Information

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

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

Specify allowed data types.

Settings

Default: Off

On

Requires all data port inputs to have the same data type.

Off

Allows data port inputs to have different data types.

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 propagate the input signal size to the output signal.

Off

Requires that input signals 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 outputs.

Settings

Default: []

The default value is [] (unspecified).

Simulink uses this value to perform:

  • Simulation range checking (see Signal Ranges)

  • Automatic scaling of fixed-point data types

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 the block outputs.

Settings

Default: []

The default value is [] (unspecified).

Simulink uses this value to perform:

  • Simulation range checking (see Signal Ranges)

  • Automatic scaling of fixed-point data types

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

Simulink chooses a data type to balance numerical accuracy, performance, and generated code size, while taking into account the properties of the embedded target hardware. If you change the embedded target settings, the data type selected by the internal rule might change. It is not always possible for the software to optimize code efficiency and numerical accuracy at the same time. If the internal rule doesn't meet your specific needs for numerical accuracy or performance, use one of the following options:

  • Specify the output data type explicitly.

  • Explicitly specify a default data type such as fixdt(1,32,16) and then use the Fixed-Point Tool to propose data types for your model. For more information, see fxptdlg.

  • To specify your own inheritance rule, use Inherit: Inherit via back propagation and then use a Data Type Propagation block. Examples of how to use this block are available in the Signal Attributes library Data Type Propagation Examples block.

Inherit: Inherit via back propagation

Uses the data type of the driving block.

double

Specifies output data type double.

single

Specifies output data type single.

int8

Specifies output data type int8.

uint8

Specifies output data type uint8.

int16

Specifies output data type int16.

uint16

Specifies output data type uint16.

int32

Specifies output data type int32.

uint32

Specifies output data type uint32.

fixdt(1,16,0)

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

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

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

<data type expression>

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

Command-Line Information

See Block-Specific Parameters for the command-line 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.

Expression

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

Dependency

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

Dependency

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.

Dependency

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.

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.

Dependency

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.

Slope

Specify slope for the fixed-point data type.

Settings

Default: 2^0

Specify any positive real number.

Dependency

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.

Bias

Specify bias for the fixed-point data type.

Settings

Default: 0

Specify any real number.

Dependency

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.

Bus Support

The Multiport 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 Multiport Switch block must have the same sample time. This requirement holds even when 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 Multiport Switch block. For details about defining and using an array of buses, see Combine Buses into an Array of Buses. When you use an array of buses with a Multiport Switch block, set Number of data ports to a value of 2 or greater.

Examples

Zero-Based Indexing for Data Ports

The sf_aircontrolsf_aircontrol model uses a Multiport Switch block in the Physical Plant subsystem. This block uses zero-based indexing for contiguous ordering of three data ports.

The indices are visible on the data port labels. You do not have to open the block dialog box to determine whether the data ports use zero-based or one-based indexing.

When you set Data port for default case to Last data port, the last data port includes a * on the label. The comma after the * indicates that the data port index has a value. This port corresponds to the default case, which applies when the control input does not match the data port indices 0, 1, or 2. In this case, the Multiport Switch block outputs a value of –0.1.

One-Based Indexing for Data Ports

The sf_semantics_hotel_checkinsf_semantics_hotel_checkin model uses a Multiport Switch block. This block uses one-based indexing for contiguous ordering of three data ports.

If you increase the size of the block icon, the indices are visible on the data port labels. You do not have to open the block dialog box to determine whether the data ports use zero-based or one-based indexing.

Enumerated Names for Data Port Indices

The sldemo_fuelsyssldemo_fuelsys model uses a Multiport Switch block in the fuel_rate_control/fuel_calc/feedforward_fuel_rate subsystem. This block uses the enumerated type sld_FuelModes to specify three data port indices: LOW, RICH, and DISABLED.

When you set Data port for default case to Last data port, the last data port includes a * on the label. The comma and ellipsis after the * indicate that the data port index has a value. This port corresponds to the default case, which applies when the control input does not match the data port indices LOW, RICH, or DISABLED. In this case, the Multiport Switch block outputs a value of 0.

Noncontiguous Values for Data Port Indices

The following modelmodel uses a Multiport Switch block that specifies noncontiguous integer values for data ports.

The values of the indices are visible on the data port labels. You do not have to open the block dialog box to determine which value maps to each data port.

When you set Data port for default case to Additional data port, an extra port with a * label appears. This port corresponds to the default case, which applies when the control input does not match the data port indices 3, 5, 0, or 18. In this case, the Multiport Switch block outputs a value of 1.

Behavior for an Index Vector Block

The following modelmodel uses two Index Vector blocks that specify zero-based and one-based indexing, respectively.

If the value of the control input is not an integer, the block truncates the value by rounding to floor. For more information, see How the Block Interprets the Control Input.

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

No

See Also

Switch

Was this topic helpful?