Work with Arrays of Buses
Set Up Model for Arrays of Buses
Setting up a model to use an array of buses usually involves these basic tasks:
Define the array of buses (seeDefine an Array of Buses).
Add a subsystem for performing iterative processing on each element of the array of buses. For example, use aFor Each Subsystemblock or anIteratorblock. Connect the array of buses signal from theConcatenateblock to the iterative processing subsystem. SeePerform Iterative Processing.
Model your scalar algorithm within the iterative processing subsystem (for example, a For Each subsystem).
Operate on the array of buses (usingSelectorandAssignmentblocks).
Use theBus SelectorandBus Assignmentblocks to select elements from, or assign elements to, a scalar bus within the subsystem.
SeeAssign Values into an Array of BusesandSelect Bus Elements from an Array of Buses.
Optionally, import or log array of buses data. SeeImport Array of Buses DataandLog Arrays of Buses
The resulting model includes these components.
Perform Iterative Processing
You can perform iterative processing on the bus signal data of an array of buses using blocks such as aFor Each Subsystemblock, aWhile Iterator Subsystemblock, or aFor Iterator Subsystemblock. 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:
ABus Assignment块赋值总线元素
AnAssignmentblock to assign the bus to the array of buses
Use anAssignmentblock to assign values to specified elements in a bus array.
For example, in thesldemo_bus_arrays
model, theAssignmentblock assigns the value to the first element of the array of buses.
To assign bus elements within a bus signal, use theBus Assignmentblock. The input for theBus Assignmentblock must be a scalar bus signal.
Assign into Arrays of Buses
You can use aBus Assignmentblock 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 aMATLAB Functionblock.
For example, suppose that you have this bus structure:
The bus has achildren
element, which is a sub-bus array. This example shows how to assign to elementc
and to elementa
. TheInportandOutportblocks use theParent
bus object. To define the assignments, this example uses aMATLAB Functionblock, because you cannot assign into elementa
using aBus AssignmentorAssignmentblock.
TheMATLAB Functionblock uses this function code for making the assignments:
functiony = fcn(u) y = u; y.c = false;foridx = 1:length(y.children) y.children(idx).a = int32(zeros(5, 1));end
Select Bus Elements from an Array of Buses
To select a signal within an array of buses, use a:
Selectorblock to find the appropriate bus within the array of buses.
Bus Selectorblock to select the signal.
Use aSelectorblock to select elements of an array of buses. The input array of buses can have any dimension. The output bus signal of theSelector块是一个选择或再保险ordered set of elements from the input array of buses.
For example, thesldemo_bus_arrays
model uses Selector blocks to select elements from the array of buses signal that theAssignmentandFor Each Subsystemblocks outputs. In this example, here is the Block Parameters dialog box for theSelectorblock that selects the first element:
To select bus elements within a bus signal, use theBus Selectorblock. The input for theBus Selectorblock must be a scalar bus signal.
Import Array of Buses Data
Use a rootInportblock to import (load) an array of structures of MATLAB®timeseries
objects for an array of buses. You can import partial data into the array of buses.
For details, seeImport Array of Buses Data.
You cannot use aFrom WorkspaceorFrom Fileblock to import data for an array of buses.
Log Arrays of Buses
To export an array of buses signal, mark the signal for signal logging. For more information, seeSave Run-Time Data from Simulation.
Note
金宝app®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, seeAccess Array of Buses Signal Logging Data.
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 typesMyData
andPressureBus
.
Suppose that you set the data type of the signal elementtemperature
toint16
, and the data type of the elementss1
ands2
todouble
.
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;
The variableinitValues
provides initial conditions for a signal that is an array of three buses. You can useinitValues
to specify theInitial conditionparameter of a block such asUnit Delay.
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 specifyinitStruct
in theInitial conditionparameter of a block, each bus in the array uses the same initial value,15
, for the signal elementtemperature
. Similarly, the buses use the initial value10.32
for the elementpressure.s1
and the value9.46
for the elementpressure.s2
.
To create an array of structures for a bus that uses a large hierarchy of signal elements, consider using the functionSimulink.Bus.createMATLABStruct
.
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 bus objects
MyData
andPressureBus
.In the hierarchy pane, select the bus element
pressure
. Set theDimensionsproperty to[1 3]
.Create an array of four initialization structures by using the function
Simulink.Bus.createMATLABStruct
. Store the array in the variableinitStruct
. Initialize all the individual signals to the ground value,0
.initStruct=Simulink.Bus.createMATLABStruct('MyData',[],[1 4]);
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
temperature
andpressure
.To inspect a
pressure
, double-click one of the fields.The value of each of the four
pressure
fields is an array of three substructures. Each substructure has the fieldss1
ands2
.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
s1
of the second substructurepressure
在third structure ofinitStruct
, use this code:initStruct(3).pressure(2).s1 = 15.35;
Code Generation
Code generation for arrays of buses produces structures with a specific format. SeeCode Generation for Arrays of Buses.