Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 36 ยท Signals and Subsystems

Simulink Signal Routing: Mux, Demux, Switch, Goto/From and Buses

Learn the Simulink signal routing blocks: Mux, Demux, Selector, Switch, Multiport Switch, Merge, Goto and From, and how to group signals with Bus Creator and Bus Selector.

As models grow, wiring becomes the hard part. Dozens of signals must travel between subsystems, be grouped, chosen between and kept tidy. Simulink's Signal Routing library has blocks for each of these jobs. This lesson explains the common ones, when to use a bus instead of a bundle of lines, and the traps that make routing confusing.

Simulink is not available on this site, so the blocks are described from the product documentation. Python stand-ins show what muxing, switching and bus selection do to data.

Combining and splitting signals

Block Purpose
Mux Combines several scalar or vector signals into one vector
Demux Splits a vector into several outputs
Vector Concatenate / Matrix Concatenate Joins arrays along a dimension
Selector Picks elements by index ([1 3]) from a vector or matrix
Reshape Changes the shape without changing the elements
Bus Creator Groups different signals (different types or sizes) into a bus
Bus Selector Takes named signals out of a bus

A Mux bundle must contain signals of the same data type. A bus may mix types and dimensions, and keeps signal names. That is why buses are recommended for interfaces between components: add a signal to the bus and nothing else is renumbered.

Vector or bus?

Vector (Mux) Same type, indexed
Bus Mixed types, named
Selector Pick by index
Bus Selector Pick by name

Use buses at subsystem boundaries.

1 / 2

Buses in practice

  1. Place a Bus Creator, set Number of inputs to 3 and connect the signals. Name the signals on the lines; the names become the bus elements.
  2. Downstream, place a Bus Selector. Its dialog lists the elements of the incoming bus; move the ones you need to the right-hand list.
  3. For reusable interface definitions use a bus object defined in MATLAB (or a data dictionary):
MATLAB
elems(1) = Simulink.BusElement; elems(1).Name = 'speed'; elems(1).DataType = 'single';
elems(2) = Simulink.BusElement; elems(2).Name = 'gear';  elems(2).DataType = 'uint8';
VehicleBus = Simulink.Bus;
VehicleBus.Elements = elems;

Then set the Bus Creator's Output data type to Bus: VehicleBus and use the same type on Inport and Outport blocks. A Bus Element In/Out pair (R2019a and later) lets you add elements right on subsystem ports without placing creators and selectors manually.

Buses can be nested: a bus can contain another bus. A virtual bus exists only for drawing; a nonvirtual bus (a struct in generated code) is a contiguous block of memory with a defined layout. Bus-capable blocks such as Unit Delay and Switch can pass an entire bus through.

Choosing between signals

Block Rule
Switch Three inputs: data input 1, control input 2, data input 3. Output = input 1 if the control signal passes the Criteria (u2 >= Threshold, u2 > Threshold, u2 ~= 0), otherwise input 3
Multiport Switch Control input is an integer index selecting among N data inputs
Manual Switch You click the block to toggle; for experiments
Merge Combines outputs of conditionally executed subsystems (only one is active at a time) into one signal
If / Switch Case Create action signals that run subsystems conditionally

The middle port of a Switch is the control input, which surprises beginners. If your model picks the wrong branch, check which port your control signal is plugged into.

Goto and From

A Goto block with Tag speed and a From block with the same tag form a wireless connection: no line crosses the diagram. Use them sparingly; many Goto/From pairs create a "teleporting" diagram that is hard to follow. Scope can be local (same level), scoped (within a Goto Tag Visibility block's region) or global. For data that many blocks need, Data Store Memory with Data Store Read/Write is the explicit way to share state, but it can hide dependencies as well.

See what they do

Python Stand-in: Mux, Demux, Selector (Python)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Stand-in: Switch and Bus Selector (Python)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice how the bus carries a float, an integer and a boolean at once, which a vector could not do.

Routing tips

  • Make lines branch by Ctrl+dragging from a line or by dragging from the middle of an existing line; do not duplicate a source.
  • Keep Mux and Demux counts consistent. A Demux with the wrong number of outputs gives a dimension error.
  • Avoid very large buses at the top level; group by function (sensors, commands, status).
  • Switch between a vector Mux and a Bus by testing the use case: if elements have different units or types, use a bus.
  • Never feed a Merge block from unconditional signals; Merge expects at most one active input.

A Goto/From pair crossing atomic or referenced model boundaries does not work: signals cannot teleport out of a model reference. Use ports and buses instead.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which block lets you group signals of different data types while keeping names?

    A bus can contain signals of different types and sizes, each named.
  2. 2. In a Simulink Switch block, which input is the control signal?

    Input 2 in the middle is the control input; inputs 1 and 3 are data.
  3. 3. Which block merges outputs of conditionally executed subsystems into one signal?

    Merge combines outputs of conditionally executed subsystems of which only one is active.
  4. 4. A Selector with indices [4 2] applied to [10 20 30 40] returns...

    Index 4 gives 40, then index 2 gives 20, in that order.

Summary

  • Mux/Demux build and split same-type vectors; Selector and Reshape pick and reshape.
  • Buses group differently typed, named signals and are the right interface between subsystems.
  • Switch uses its middle input as control; Multiport Switch uses an integer; Merge joins conditionally executed outputs.
  • Goto/From avoids long lines but hides flow; use sparingly.

Next: Subsystems and Conditional Execution