Work with Arrays of Buses
Setting up a model to use an array of buses usually involves these basic 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. For example, use a For Each Subsystem block or an Iterator block. Connect the array of buses from the Concatenate block to the iterative processing subsystem. See Perform Iterative Processing.
Model your scalar algorithm within the iterative processing subsystem (for example, a For Each 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.
The resulting model includes these components.
Perform Iterative Processing
You can perform iterative processing on the bus data of an array of buses using blocks such as a For Each Subsystem block, a While Iterator Subsystem block, or a For Iterator Subsystem block. 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 an Array of Buses
To assign a value to a signal within an array of buses, use:
A Bus Assignment block to assign a value to a bus element
An Assignment block to assign the bus to the array of buses
Use an Assignment block to assign values to specified elements in a bus array.
For example, in the
sldemo_bus_arrays model, the
Assignment block assigns the value to the first element of the
array of buses.
To assign bus elements within a bus, use the Bus Assignment block. The input for the Bus Assignment block must be a bus.
Assign into Arrays of Buses
You can use a Bus Assignment block to assign or fully replace a nested bus that is an array of buses. To assign the data for a nested bus inside an array of buses or to make a partial assignment to certain elements with the array of buses, you can use a MATLAB Function block.
For example, suppose that you have this bus structure:
The bus has a
children element, which is a sub-bus array.
This example shows how to assign to element
c and to element
a. The Inport and Outport
blocks use the
Bus object. To define the assignments, this example uses a
MATLAB Function block, because you cannot assign into element
a using a Bus Assignment or
The MATLAB Function block uses this function code for making the assignments:
function y = fcn(u) y = u; y.c = false; for idx = 1:length(y.children) y.children(idx).a = int32(zeros(5, 1)); end
Select Bus Elements from Array of Buses
To select a bus element from an array of buses, use a:
Selector block to select the appropriate bus within the array of buses.
Bus Selector block to select the bus element within the bus.
Use a Selector block to select elements of an 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.
For example, the
sldemo_bus_arrays model uses
Selector blocks to select elements from the array of buses that
the Assignment and For Each Subsystem blocks outputs.
In this example, here is the Block Parameters dialog box for the
Selector block that selects the first element:
To select bus elements within a bus, use the Bus Selector block. The input for the Bus Selector block must be a bus.
Import Array of Buses Data
Use a root Inport block to import (load) an array of structures of
timeseries objects for an array of buses. You can import partial
data into the array of buses.
For details, see Import Array of Buses Data.
You cannot use a From Workspace or From File block to import data for an array of buses.
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.
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 Array of Buses Signal Logging Data.
Root level bus outputs are not logged when you select the Output configuration parameter. Use standard signal logging instead, as described in Export Signal Data Using Signal Logging.
Do not use signal logging for bus 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.
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.
Here is an example that shows how to initialize an array of buses. Suppose that
you define the bus types
Suppose that you set the data type of the signal element
int16, and the data type of
To specify initial conditions for an array of buses, you can create a variable whose value is an array of initial condition structures.
initValues(1).temperature = int16(5); initValues(1).pressure.s1 = 9.87; initValues(1).pressure.s2 = 8.71; initValues(2).temperature = int16(20); initValues(2).pressure.s1 = 10.21; initValues(2).pressure.s2 = 9.56; initValues(3).temperature = int16(35); initValues(3).pressure.s1 = 8.98; initValues(3).pressure.s2 = 9.17;
initValues provides initial conditions for a
signal that is an array of three buses. You can use
specify the Initial condition parameter of a block such as
Alternatively, you can use a single scalar structure to specify the same initial conditions for all the buses in the array.
initStruct.temperature = int16(15); initStruct.pressure.s1 = 10.32; initStruct.pressure.s2 = 9.46;
If you specify
initStruct in the Initial
condition parameter of a block, each bus in the array uses the same
15, for the signal element
temperature. Similarly, the buses use the initial value
10.32 for the element
pressure.s1 and the
9.46 for the element
To create an array of structures for a bus that uses a large hierarchy of signal
elements, consider using the function
This example shows how to initialize a nested array of buses. Create an initial condition structure for a complicated signal hierarchy that includes nested arrays of buses.
In the Bus Editor, create the
In the hierarchy pane, select the bus element
pressure. Set the Dimensions property to
Create an array of four initialization structures by using the function
Simulink.Bus.createMATLABStruct. Store the array in the variable
initStruct. Initialize all the individual signals to the ground value,
In the base workspace, double-click the variable
initStructto view it in the variable editor.
The four structures in the array each have the fields
To inspect a
pressure, double-click one of the fields.
The value of each of the four
pressurefields is an array of three substructures. Each substructure has the fields
To provide unique initialization values for the signals in an array of buses, you can specify the values manually using the variable editor.
Alternatively, you can write a script. For example, to access the field
s1of the second substructure
pressurein the third structure of
initStruct, use this code:
initStruct(3).pressure(2).s1 = 15.35;
Structure Parameter Requirements
To initialize an array of buses with structure parameters, you can use:
0. In this case, all the individual signals in the array of buses use the initial value
structthat 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:
Numeric data type, such as
You cannot use partial structures.
Code generation for arrays of buses produces structures with a specific format. See Code Generation for Arrays of Buses.