Work with Arrays of Buses
Setting up a model to use an array of buses usually involves these tasks:
Define the array of buses. See Create Array of Buses from Nonvirtual Buses.
Add a subsystem for performing iterative processing on each element of the array of buses. See Perform Iterative Processing.
Model your scalar algorithm within the iterative processing subsystem.
Operate on the array of buses (using Selector and Assignment blocks).
Use the Bus Selector and Bus Assignment blocks to select elements from, or assign elements to, a nonvirtual bus within the subsystem.
See Assign Values into Arrays of Buses and Select Bus Elements from Array of Buses.
Optionally, import or log array of buses data. See Load Data for Arrays of Buses and Log Arrays of Buses.
The resulting model creates the array of buses and uses a subsystem to iteratively process the elements of the array.
Perform Iterative Processing
You can perform iterative processing on the bus data of an array of buses using blocks such as the For Each Subsystem, While Iterator Subsystem, and For Iterator Subsystem blocks. You can use one of these blocks to perform the same kind of processing on:
Each bus in the array of buses
A selected subset of buses in the array of buses
Assign Values into Arrays of Buses
How you assign values to one or more elements in an array of buses depends on your modeling requirement.
To assign values to specified buses in an array of buses, use an Assignment block. For an example, see Model Arrays of Buses.
To assign values to specified elements in a bus, use a Bus Assignment block. Access the bus in a subsystem such as a for-each subsystem or by selecting a bus from the array of buses with a Selector block. For an example, see Assign Signal Values to Bus Elements.
A Bus Assignment block can also assign or fully replace an array of buses that is an element of a bus.
To assign values to elements of a bus in an array of buses, you can use a MATLAB Function block. For an example, see Assign into Elements of Array of Buses.
Select Bus Elements from Array of Buses
To select a bus element from an array of buses:
Use a Selector block to select a bus from the array of buses. The input array of buses can have any dimension. The output bus of the Selector block is a selected or reordered set of elements from the input array of buses.
Use a Bus Selector block to select a bus element from the bus. The input for the Bus Selector block must be a bus.
For an example, see Model Arrays of Buses.
Load Data for Arrays of Buses
Several blocks support loading external input data for an array of buses into your model for simulation. The table summarizes array of buses support for loading blocks.
Block | Array of Buses Support |
---|---|
Inport | Supported. For more information, see Load Bus Data to Root-Level Input Ports. |
In Bus Element | Supported. You cannot select elements of the array of buses. You must load the entire array of buses. For more information, see In Bus Element. |
Signal Editor | Supported. |
From Workspace | Supported. For more information, see Load Data Using the From Workspace Block. |
Playback | Supported. When you load bus data, the Playback block assigns a port to each element of the bus. |
From File | Not supported. |
From Spreadsheet | Not supported. |
Log Arrays of Buses
To export an array of buses, mark the signal for signal logging. For more information, see Save Run-Time Data from Simulation.
Note
Simulink® does not log signals inside referenced models in rapid accelerator mode.
To access the signal logging data for a specific signal in an array of buses, navigate through the structure hierarchy and specify the index to the specific signal. For details, see Access Bus Data Logged Using Dataset Format.
Root level bus outputs are not logged when you select the Output configuration parameter. Use standard signal logging instead, as described in Save Signal Data Using Signal Logging.
Do not use signal logging for buses or arrays of buses directly from within a for-each subsystem. Either use a Bus Selector block to select the bus element signals to log or add an Outport block outside of the subsystem and then log that signal. For details, see Log Signals in For-Each Subsystems.
Note
Signal logging is supported for arrays of buses with variable-size signals only when running a simulation in normal mode. (since R2023a)
Initialize Arrays of Buses
To specify a unique initial value for each of the individual signals in an array of buses, you can use an array of initial condition structures. Each structure in the array initializes one of the buses.
To initialize an array of buses with structure parameters, you can use:
The number
0
. In this case, all the individual signals in the array of buses use the initial value0
.A scalar
struct
that represents the same hierarchy of fields and field names as the bus type. In this case, the scalar structure expands to initialize each of the individual signals in the array of buses.An array of structures that specifies an initial value for each of the individual signals in the array of buses.
If you use an array of structures, all the structures in the array must have the same hierarchy of fields. Each field in the hierarchy must have the same characteristics across the array:
Field name
Numeric data type, such as
single
orint32
Complexity
Dimensions
You cannot use partial structures.
For examples, see Initialize Buses and Arrays of Buses Using MATLAB Structure from Type Editor and Programmatically Initialize Arrays of Buses.
Code Generation
Code generation for arrays of buses produces structures with a specific format. See Code Generation for Arrays of Buses.
Note
Code generation is not supported for arrays of buses with variable-size signals.