Main Content

Create Nonvirtual Buses

A nonvirtual bus is analogous to a struct in C code. Use nonvirtual buses to:

  • Package bus data as structures in generated C code.

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

Nonvirtual buses require a Simulink.Bus object data type. To simulate a model that contains nonvirtual buses, the referenced Bus objects must be in the base workspace or a data dictionary used by the model. For more information, see Specify Bus Properties with Simulink.Bus Object Data Types.

Nonvirtual buses also require all elements of the bus to have the same sample time. For more information, see Modify Sample Times for Nonvirtual Buses.

The way to create nonvirtual buses differs based on the location of the bus and the block that creates 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 Within a Component

You can use Bus Creator blocks to create nonvirtual buses within components.

Open and compile the example model, which uses Bus Creator blocks to create a hierarchy of virtual buses. To compile the model, on the Modeling tab of the Simulink Toolstrip, click Update Model or Run. Compiling the model updates the line styles, which you can use to visually identify buses.

To create this model, see Group Signal Lines Within a Component.

The virtual buses in this model are not defined by Simulink.Bus objects. To change the output of the Bus Creator blocks to nonvirtual buses, you must have Bus objects that match the bus hierarchy.

To create the Bus objects that correspond to TopBus and NestedBus, use the Simulink.Bus.createObject function. In the MATLAB Command Window, enter:

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

This function creates the Bus object that corresponds to the output bus of the specified block. If the output bus contains nested buses, the function also creates Bus objects that correspond to the nested buses. In this example, the function creates two Bus objects that are named after the corresponding buses, TopBus and NestedBus.

To view the Bus objects, open the Bus Editor. In the MATLAB Command Window, enter:

buseditor

Now that you have Bus objects that correspond to the nonvirtual buses you want to create, create the nonvirtual buses. In the Simulink Editor, double-click the Bus Creator block named Bus Creator1. In the Block Parameters dialog box, set Output data type to Bus: TopBus, select the Output as nonvirtual bus check box, and click OK. TopBus is now a nonvirtual bus, while NestedBus remains a virtual bus. To identify the nonvirtual bus by line style, compile the model.

Double-click the block named Bus Creator. In the Block Parameters dialog box, set Output data type to Bus: NestedBus, select the Output as nonvirtual bus check box, and click OK. NestedBus is now a nonvirtual bus. To update its line style, compile the model.

If you do not save the Bus objects, then you must recreate the Bus objects when you reopen the model. For information on how to save the Bus objects, see Specify Bus Properties with Simulink.Bus Object Data Types.

Create Nonvirtual Buses at Interfaces

To create a nonvirtual bus at an interface, connect the bus elements to Out Bus Element blocks and specify that the output bus is nonvirtual.

Open the example model, which uses three Out Bus Element blocks to create a virtual bus at the output port, OutBus.

To create this model, see Connect Multiple Output Signals to a Port.

Nonvirtual buses require a Simulink.Bus object data type. Create the Bus object data type that corresponds to the output bus.

  1. In the Simulink Toolstrip, on the Modeling tab, in the Design gallery, select Bus Editor.

  2. In the Bus Editor, create two Bus objects by clicking Add Bus twice.

  3. Name the Bus objects. Select one of the Bus objects, and set Name to OutBus. Select the other Bus object, and set Name to NestedBus.

  4. For each Bus object, create the Simulink.BusElement objects that correspond to the elements of the buses. Select NestedBus, then click Add/Insert BusElement twice. Name the elements Sine and Step. Select OutBus, then click Add/Insert BusElement twice. Name the elements Chirp and NestedBus.

  5. Specify a Bus object data type for any nested buses. With the NestedBus BusElement object selected, set Data type to Bus: NestedBus, then click Apply.

To output a nonvirtual bus, specify the Bus object data type and bus virtuality.

  1. Open the dialog box for the port by double-clicking one of the Out Bus Element blocks.

  2. In the tree hierarchy of the dialog box, pause on OutBus, and click the pencil icon that appears next to OutBus.

  3. Specify the Bus object data type. Set Data type to Bus: OutBus.

  4. In the tree hierarchy of the dialog box, click the signal attributes that now appear next to OutBus.

  5. Specify the bus virtuality. Set Bus virtuality to nonvirtual, then close the dialog box.

The output of the model is a nonvirtual bus. To see the nonvirtual 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, on the Modeling tab of the Simulink Toolstrip, click Update Model or Run.

If you do not save the Bus objects, then you must recreate the Bus objects when you reopen the model. For information on how to save the Bus objects, see Specify Bus Properties with Simulink.Bus Object Data Types.

Root (top-level) Inport and Outport blocks can also create nonvirtual buses at interfaces. To specify the bus virtuality:

  • For a root Inport block, on the Signal Attributes tab of the dialog box, select Output as nonvirtual bus.

  • For a root Outport block, on the Signal Attributes tab of the dialog box, select Output as nonvirtual bus in parent model.

In Bus Element blocks use the Bus virtuality parameter to determine whether they inherit or define the bus virtuality. If the block defines the bus virtuality and the virtuality of the input bus does not match, compiling the model produces an error.

Convert Virtual Bus to Nonvirtual Bus

You can convert a virtual bus to a nonvirtual bus anywhere within a model by using a Signal Conversion block.

Open and compile the example model, which contains a virtual bus hierarchy created by Bus Creator blocks. To compile the model, on the Modeling tab of the Simulink Toolstrip, click Update Model or Run. Compiling the model updates the line styles, which you can use to visually identify buses.

To define the buses and support conversion to nonvirtual buses, the Bus Creator blocks specify Simulink.Bus object data types. To create the Bus objects in the base workspace when the model is loaded, the model uses the PreLoadFcn callback.

To view the callback, in the Simulink Toolstrip, on the Modeling tab, click Model Settings > Model Properties. In the Model Properties dialog box, open the Callbacks tab and select the PreLoadFcn model callback.

To convert a virtual bus to a nonvirtual bus, use a To Nonvirtual Bus block, which is a preconfigured version of the Signal Conversion block that has Output set to Nonvirtual bus.

In the example model:

  1. Add a To Nonvirtual Bus block to the model.

  2. Drag the To Nonvirtual Bus block onto the line between the Bus Creator1 and Scope blocks. When you drag the block onto the line, the block connects to the line at both ends.

Suppose the data type of the bus you want to convert is not specified by a Bus object. You would need to perform these additional actions:

  1. Creating the corresponding Bus objects.

  2. Specifying the Bus object data type for the bus. For example, setting the Data type parameter of the To Nonvirtual Bus block to the Bus object that corresponds to the input bus.

The input to the To Nonvirtual Bus block is a virtual bus, and the output is a nonvirtual bus. To see the nonvirtual bus line style, compile the model.

Create Nonvirtual Bus from MATLAB Structure

You can use a Constant block to compactly represent a nonvirtual bus with constant-valued elements.

On the Constant block, specify:

  • A MATLAB structure for Constant value.

  • A Simulink.Bus object as the Output data type.

Constant blocks only support MATLAB structures when the output data type is a Bus object.

For example, define a MATLAB structure named const_param_struct with elements named Offset, Gain, and Threshold.

const_param_struct.Offset = 197;
const_param_struct.Gain = 4.32;
const_param_struct.Threshold = 795.68;

Define the corresponding Bus object data type.

Simulink.Bus.createObject(const_param_struct)

The new Bus object uses the default name slBus1.

Open and simulate the example model, which contains a Constant block with Constant value set to const_param_struct and Output data type set to Bus: slBus1.

open_system('NonvirtualBusFromStructModel')
sim('NonvirtualBusFromStructModel');

The output of the Constant block is a nonvirtual bus.

See Also

Blocks

Objects

Related Topics