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:
- Reduce maintenance by centralizing algorithms used for processing multiple buses.
- Streamline iterative processing of multiple buses of the same type, for example, by using a for-each subsystem with the array of buses.
- Change the number of buses being processed without having to restructure the parts of the model that use the array of buses.
- Use built-in blocks, such as the Assignment or Selector blocks, to manipulate arrays of buses just like arrays of any other type. Using an array of buses avoids the need for you to create custom S-functions to manage packing and unpacking structure signals.
- Generate code that has an array of C structures, which you can integrate with legacy C code that uses arrays of structures. This approach simplifies indexing into an array for SimulinkĀ® computations, using a
for
loop on indexed structures.
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:
- Be nonvirtual
- Specify the same Simulink.Bus object data type (that is, the same name, hierarchies, and bus element attributes)
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:
- Define one
Simulink.Bus
object data type for all of the nonvirtual buses that you want to group in the array of buses. - 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.
- Connect the nonvirtual buses to the block inputs.
The block output is the array of buses.
Open and simulate the example model named ArrayOfBuses
.
To demonstrate how to create arrays of buses from nonvirtual buses:
- Two Bus Creator blocks create nonvirtual buses that have the same
Bus
object data type (Bus: BusObject
). - A Vector Concatenate block with Number of inputs set to
2
groups the two nonvirtual buses in an array of buses.
To demonstrate how to select elements from the array of buses:
- A Selector block extracts one of the buses from the array of buses based on the Index specified in the dialog box. Because arrays of buses are concatenated signals that provide index-based access to their elements, the Selector block uses
Index vector (dialog)
for the Index Option. - A Bus Selector block extracts the elements of the nonvirtual bus using name-based access.
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:
- Set Constant value to an array of MATLABĀ® structures or a
Simulink.Parameter
object that specifies an array of MATLAB structures. - Set Output data type to a
Simulink.Bus
object.
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");
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.