Group Nonvirtual Buses in Arrays of Buses - MATLAB & Simulink (original) (raw)

An array of buses is an array whose elements are nonvirtual buses defined by the same Simulink.Bus object. An array of buses is equivalent to a MATLABĀ® array of structures.

Use an array of buses to:

For example, you can model a multi-channel system, such as a communications system, as an array of buses. While all the channels have the same properties, each of the channels may have a different value.

Tip

To create a reusable specification for an array of buses, define a Simulink.ValueType object with DataType set to aSimulink.Bus object and Dimensions set to the dimensions of the array.

For a model that demonstrates multiple ways to use an array of buses, see Model Arrays of Buses.

Arrays of buses with variable-size signals are supported, subject to certain limitations. See Variable-Size Signal Limitations.

Requirements of Arrays of Buses

All buses combined into an array of buses must:

For information on creating nonvirtual buses, see Create Nonvirtual Buses.

For information on which blocks support arrays of buses, see Bus-Capable Blocks.

Create Array of Buses from Nonvirtual Buses

You can use a Vector Concatenate or Matrix Concatenate block to group nonvirtual buses into an array of buses. Simulink treats nonvirtual buses as scalars. Therefore, you can use either of these blocks regardless of the bus element data types.

To create an array of buses with one of these blocks:

  1. Define one Simulink.Bus object data type for all of the nonvirtual buses that you want to group in the array of buses.
  2. In the Block Parameters dialog box for the Vector Concatenate or Matrix Concatenate block, set Number of inputs to the number of buses you want in the array of buses. The block icon displays the number of input ports that you specify.
  3. Connect the nonvirtual buses to the block inputs.

The block output is the array of buses.

Open and simulate the example model named ArrayOfBuses.

ArrayOfBuses model

To demonstrate how to create arrays of buses from nonvirtual buses:

To demonstrate how to select elements from the array of buses:

The Signal Dimensions information overlay shows that the array of buses contains two buses. To enable the Signal Dimensions overlay, in the Simulink Toolstrip, on the Debug tab, select Information Overlays > Signal Dimensions.

Create Array of Buses from MATLAB Structures

You can use a Constant block to compactly represent an array of buses with constant-valued bus elements. This technique can reduce the number of lines in a model and the number of variables used by the model, especially when the model repeats an algorithm with different parameter values.

To create an array of buses, on a Constant block:

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

Open and compile the example model, which contains a Constant block that creates a nonvirtual bus. Compiling the model updates the line styles, which you can use to visually identify arrays of buses. To compile the model, on the Modeling tab of the Simulink Toolstrip, click Update Model or Run. Alternatively, in the MATLAB Command Window, enter these commands.

mdl = "ArrayOfBusesFromStructure"; open_system(mdl) set_param(mdl,SimulationCommand="Update");

ArrayOfBusesFromStructure model

The PreLoadFcn model callback defines an array of structures named busval. Each structure is composed of elements named offset, gain, and threshold. Then, the model callback uses the Simulink.Bus.createObject function to create a Simulink.Bus object from the structure. The bus object uses the default name slBus1.

To view the model callback, in the Simulink Toolstrip, on the Modeling tab, in the Design gallery, select Property Inspector. Model callbacks appear in the Property Inspector when nothing is selected at the top level of a model. Alternatively, to get the model callback programmatically, use the get_param function.

cb = get_param(mdl,"PreLoadFcn")

cb = '% Define an array of structures named busval. Compose each % structure of elements named offset, gain, and threshold.

 busval(1).offset = 197;
 busval(1).gain = 4.32;
 busval(1).threshold = 795.68;
 
 busval(2).offset = 158;
 busval(2).gain = 3.83;
 busval(2).threshold = 1039.77;
 
 % Define the corresponding Simulink.Bus object.
 
 Simulink.Bus.createObject(busval)'

To create an array of structures for a bus hierarchy with many elements, consider using the Simulink.Bus.createMATLABStruct function.

To create an array of buses, the Constant block sets Constant value to busval and Output data type to Bus: slBus1.

The For Each Subsystem block iteratively processes the constant values for each nonvirtual bus in the array. The subsystem contents display the nonvirtual bus instead of the array of buses that connects to the port.

For-each subsystem contents

See Also

Blocks

Topics