Main Content

Create Nonvirtual Buses

A nonvirtual Simulink® bus is analogous to a struct in C code. To package bus data as structures in generated C code, use nonvirtual buses.

You must also use nonvirtual buses to:

  • Construct an array of buses.

  • Interface with external code through an S-function.

  • Have bus data cross MATLAB Function block or Stateflow® chart boundaries.

  • Display and log buses with a Scope block.

To create a nonvirtual bus, you must:

  • Specify a Simulink.Bus object as the data type for the bus. Set Data type to Bus: <object name>, where <object name> is the Bus object name.

  • Specify that the output of the block is a nonvirtual bus. Select Output as nonvirtual bus or, for Outport blocks, Output as nonvirtual bus in parent model.

All signals in a nonvirtual bus must have the same sample time. For more information, see Modify Sample Times for Nonvirtual Buses.

To simulate a model that contains nonvirtual buses, the referenced Bus objects must be in the base workspace or a data dictionary. You must define the Bus object or use an already defined Bus object. A model callback can load the necessary Bus objects.

The way to create nonvirtual buses differs based on the location of the bus:

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

Create Nonvirtual Buses with Bus Creator Blocks

This example shows how to configure a Bus Creator block to output a nonvirtual bus.

Open and simulate the example model, which contains a virtual bus hierarchy.

Create Simulink.Bus Objects

Since the virtual buses in this model are not defined by Bus objects, you must create Bus objects that match the bus hierarchy. If the virtual buses were defined by Bus objects, you would not need to create Bus objects.

To create and save Bus objects for the buses in the model, use the Simulink.Bus.createObject function. When you specify a Bus Creator block that creates a bus that contains other buses, this function creates Bus objects for the bus created by the block and all nested buses. When you specify a file name, it saves the Bus objects in a function with that name.

busInfo = Simulink.Bus.createObject...
  ('NonvirtualBusCreationModel',...
  'NonvirtualBusCreationModel/Bus Creator1',...
  'NonvirtualBusCreationFunction');

In the base workspace, Simulink creates two Bus objects named after the corresponding buses, TopBus and NestedBus. In the current folder, Simulink creates a function named NonvirtualBusCreationFunction.m.

To see the created Bus objects, open the Bus Editor by entering:

buseditor

Specify Nonvirtual Bus Outputs

In the model, double-click the Bus Creator1 block. In the dialog box, set the Output data type to Bus: TopBus and select the Output as nonvirtual bus check box.

To identify the nonvirtual bus by line style, simulate the model.

The Bus Creator1 block output is a nonvirtual bus, while the Bus Creator block output remains a virtual bus.

Double-click the Bus Creator block. In the dialog box, set Output data type to Bus: NestedBus and select the Output as nonvirtual bus check box.

To identify the new nonvirtual bus by line style, simulate the model.

Create Nonvirtual Bus Output for Referenced Models

This example shows how to convert a virtual bus to a nonvirtual bus at the output port of a referenced model.

Open and simulate the example model, which references a model with a virtual bus output.

To define the model interface, the output port of the referenced model has a Simulink.Bus object as its data type. To create the Bus object in the base workspace when the referenced model is loaded, the referenced model uses a callback.

In the referenced model, double-click the Outport block. In the dialog box, on the Signal Attributes tab, select Output as nonvirtual bus in parent model, then click OK.

To update line styles, simulate the model again.

The input to the Outport block remains a virtual bus.

The output of the Model block is now a nonvirtual bus.

Convert Virtual Bus to Nonvirtual Bus

This example shows how to convert a virtual bus to a nonvirtual bus within a model.

Open and simulate the example model, which references a model with a virtual bus output.

To define the model interface, the output port of the referenced model has a Simulink.Bus object as its data type. To create the Bus object in the base workspace when the referenced model is loaded, the referenced model uses a callback.

To convert the virtual bus output to a nonvirtual bus, add a To Nonvirtual Bus block between the Model block and Outport block. This block is a preconfigured version of the Signal Conversion block that has the Output parameter set to Nonvirtual bus.

To identify the nonvirtual bus by line style, simulate the model.

See Also

Blocks

Objects

Related Topics