Setting up a model to use an array of buses usually involves these basic tasks:
Define the array of buses (see Define an Array of 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 signal 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 scalar bus within the subsystem.
The resulting model includes these components.
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
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 scalar bus.
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
To select a signal within an array of buses, use a:
Selector block to find the appropriate bus within the array of buses.
Bus Selector block to select the signal.
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 signal 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
To select bus elements within a bus, use the Bus Selector block. The input for the Bus Selector block must be a scalar bus.
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.
To export an array of buses signal, 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.
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
Create an array of four initialization structures by using the
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
initStruct to 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
The value of each of the four
pressure fields 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
s1 of the second substructure
pressure in the third structure of
initStruct, use this code:
initStruct(3).pressure(2).s1 = 15.35;
Code generation for arrays of buses produces structures with a specific format. See Code Generation for Arrays of Buses.