Main Content

Group Signals or Messages into Virtual Buses

To reduce line complexity and clutter in a block diagram and make incrementally changing an interface easier, use virtual buses. A virtual bus is analogous to a bundle of wires held together by tie wraps. For comparison, a nonvirtual bus is analogous to a structure in C code. You can access the bus as a whole or select specific elements from the bus.

When you create a virtual bus, group lines based on their functionality. By organizing signals or messages into logical groupings, you reduce the likelihood of significant refactoring.

Not all blocks can accept buses, and some blocks implicitly convert buses to vectors. To learn which blocks support which types of buses, see Bus-Capable Blocks. To identify bus conversions, see Identify Automatic Bus Conversions.

How you create virtual buses differs based on the location of the signals or messages that you want to group.

  • Within a component — Use Bus Creator blocks.

  • At the output interface of a component — Use Out Bus Element blocks.

  • At the input interface of a component — Use In Bus Element blocks.

To focus on fundamental steps, these examples are simple. However, buses are most useful when you have many signals or messages to group.

Tip

When you open a model or create a bus, the Simulink® Editor does not display bus line styles. To update the line styles, compile the model. In the Simulink Toolstrip, on the Modeling tab, click Update Model or Run.

To create buses at the input or output interface of a component using functions, see Programmatically Create Bus Element Ports.

Group Signal Lines Within Component

You can group signals into a virtual bus within a component by using Bus Creator blocks.

Open a new model and add three source blocks. For example, add a Chirp Signal, Sine Wave, and Step block.

Model with three source blocks

To create a bus that contains the outputs from multiple blocks, click and drag to select the blocks. For this example, select the Chirp Signal and Sine Wave blocks. In the action bar that appears, click Create Bus.

Pointer paused on Create Bus button in action bar

The software adds a Bus Creator block and connects the inputs to that block. The output of the Bus Creator block is a virtual bus.

First two source blocks connected to a Bus Creator block

To make identifying the elements of the bus easier, label the inputs to the Bus Creator block.

  • Double-click the line between the Chirp Signal block and the Bus Creator block. Then, enter Chirp.

  • Double-click the line between the Sine Wave block and the Bus Creator block. Then, enter Sine.

To create a second bus that contains the first bus and the output of the Step block, click and drag to select the Bus Creator and Step blocks. In the action bar that appears, click Create Bus. Including the Sine Wave and Chirp Signal blocks in your selection does not affect the result because these blocks provide elements of the input bus.

Pointer paused on Create Bus button in action bar

The software adds another Bus Creator block and connects the inputs to that block. The output of the Bus Creator block is a virtual bus that contains a nested bus.

First Bus Creator block and third source block connected to another Bus Creator block

You can nest buses to any depth. If one of the inputs to a Bus Creator block is a bus, then its output is a bus hierarchy that contains at least one nested bus.

Label the inputs to the new Bus Creator block.

  • Double-click the line between the Bus Creator blocks. Then, enter Sinusoidal.

  • Double-click the line between the Step block and the Bus Creator block. Then, enter Step.

Optionally, arrange the blocks to improve the readability of the model.

Connect the output of the second Bus Creator block to a Scope block and label the output Sources.

To visually identify the buses by line styles, in the Simulink Toolstrip, on the Modeling tab, click Update Model or Run.

Compiled model with bus line style at output of Bus Creator blocks

Connect Multiple Output Signals to One Port

To group the outputs of a subsystem or model into a bus, use Out Bus Element blocks. Out Bus Element blocks accept signals, messages, and buses as input.

For example, create a simple model that connects three signals to one output port.

  1. Open a new model and add a Chirp Signal, Sine Wave, and Step block.

    Model with three source blocks

  2. Add an Out Bus Element block to the model. Then, connect the Chirp Signal block to the Out Bus Element block.

    First source block connected to an Out Bus Element block labeled OutBus.signal1

  3. To make two copies of the Out Bus Element block, press Ctrl and drag the Out Bus Element block twice. To use the same port for the additional output signals, select New Element each time you copy the block.

    Menu with pointer on New Element

  4. Connect the Sine Wave and Step blocks to the Out Bus Element blocks.

    Each source block connected to a default Out Bus Element block

  5. To make identifying the elements of a port easier, rename the elements. The label next to each Out Bus Element block has two parts. The first part of the label describes the port and uses the default name OutBus. The second part of the label describes a bus element and uses the default name signal1. To create hierarchy by nesting a bus element in other buses, add a dot after each bus name.

    • Double-click signal1. Then, enter NestedBus.Chirp.

    • Double-click signal2. Then, enter NestedBus.Sine.

    • Double-click signal3. Then, enter Step.

    Out Bus Element blocks labeled OutBus.NestedBus.Chirp, OutBus.NestedBus.Sine, and OutBus.Step with a cursor after Step

The Out Bus Element blocks create a virtual bus at the output port of the model.

To inspect the bus hierarchy at the port, double-click one of the Out Bus Element blocks. The dialog box displays a top-level bus named OutBus that contains elements named NestedBus and Step. NestedBus contains elements named Chirp and Sine.

Bus hierarchy with NestedBus and Step under OutBus and Chirp and Sine under NestedBus

To see the virtual bus, reference the model in another model or move the Out Bus Element blocks into a subsystem. The line styles update when you compile the model. To compile the model, in the Simulink Toolstrip, on the Modeling tab, click Update Model or Run.

Bus output from a Model block that references the example model

Optionally, add more elements to the bus hierarchy with or without adding more Out Bus Element blocks. For more information, see Define Output Bus Without Extra Blocks or Bus Objects.

Specify Multiple Input Signals at One Port

When a model or subsystem file expects an input bus, define the port with one or more In Bus Element blocks.

Suppose an input port expects a bus that contains a nested bus named NestedBus and a signal named Step. The nested bus contains signals named Chirp and Sine. You expect to use only the signal named Step and not the nested bus or its elements.

  1. In a blank model or subsystem, add an In Bus Element block. This block creates an input port.

    Default In Bus Element block with block label that says InBus.signal1

  2. To rename the element that the block represents, in the block label, double-click signal1. Then, replace signal1 with Step.

    In Bus Element block with block label that says InBus.Step. The part of the label that says Step is in edit mode.

To define the rest of the bus hierarchy without additional In Bus Element blocks and without a Simulink.Bus object, add elements without adding blocks.

  1. Open the Property Inspector. Then, select the In Bus Element block. Alternatively, double-click the block to open a dialog box.

  2. In the Property Inspector or dialog box, select the top-level bus named InBus. Then, click the down arrow button Add element, and select Add element without block.

    Pointer paused on Add element without block option

    The new element is nested under the previously selected element. The block diagram is unchanged.

    InBus with new element named signal1

    The new element name is gray and italicized to indicate that an In Bus Element block does not represent the element directly.

  3. Double-click the new element name. Then, enter NestedBus.

    Bus hierarchy with NestedBus in edit mode

  4. To add elements to the nested bus, select NestedBus. Then, click the down arrow button Add element, and select Add element without block twice.

    NestedBus selected and pointer paused on Add element without block option

    Before R2026a: To add multiple elements to a bus, select the parent before adding each element. The selection changes to the new element instead of remaining on the parent.

  5. To rename the bus elements from signal and signal2 to Chirp and Sine, respectively, double-click each element name and enter the new name.

    Bus hierarchy with Chirp and Sine under NestedBus

Tip

When you want to add many elements without blocks, using the Simulink.Bus.addElementToPort function can be quicker than using the dialog box. For an example, see Programmatically Create Bus Element Ports.

Add Blocks for Bus Element Ports

Suppose you want to add blocks for a bus element port.

To add blocks for input elements, click an element name, or hold Ctrl as you click multiple element names. Then, click Add blocks for selected elements. For example, add blocks for Chirp and Sine.

Pointer paused on Add blocks for selected signals button

Corresponding In Bus Element blocks appear in the block diagram.

In Bus Element blocks labeled InBus.NestedBus.Chirp and InBus.NestedBus.Sine

In the block label, each dot indicates a new level of hierarchy.

For output bus element ports, the way to add blocks for elements differs slightly. To add blocks for output bus element ports, select an element that an existing Out Bus Element block does not represent. Then, add an Out Bus Element block by clicking Add block for selected element. To prevent a conflict where multiple Out Bus Element blocks represent the same element, you can add only one Out Bus Element block at a time.

For more examples and information, see In Bus Element and Out Bus Element.

Access Elements of Buses

You can access the bus as a whole or select specific signals, messages, or nested buses from the bus.

How you extract elements from a bus depends on the location of the bus.

See Also

| | |

Topics