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:

  1. Define the array of buses (see Define an Array of Buses).

  2. 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.

  3. Model your scalar algorithm within the iterative processing subsystem (for example, a For Each subsystem).

    1. Operate on the array of buses (using Selector and Assignment blocks).

    2. Use the Bus Selector and Bus Assignment blocks to select elements from, or assign elements to, a scalar bus within the subsystem.

    See Assign Values into an Array of Buses and Select Bus Elements from an Array of Buses.

  4. Optionally, import or log array of buses data. See Import Array of Buses Data and Log Arrays of Buses

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:

  1. A Bus Assignment block to assign a value to a bus element

  2. 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.

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 Parent 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 Assignment block.

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 an Array of Buses

To select a signal within an array of buses, use a:

  1. Selector block to find the appropriate bus within the array of buses.

  2. 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 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 scalar bus.

Import Array of Buses Data

Use a root Inport block 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, 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.

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 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 types MyData and PressureBus.

Suppose that you set the data type of the signal element temperature to int16, and the data type of the elements s1 and s2 to double.

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 variable initValues provides initial conditions for a signal that is an array of three buses. You can use initValues to specify the Initial condition parameter of a block such as Unit 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 specify initStruct in the Initial condition parameter of a block, each bus in the array uses the same initial value, 15, for the signal element temperature. Similarly, the buses use the initial value 10.32 for the element pressure.s1 and the value 9.46 for the element pressure.s2.

To create an array of structures for a bus that uses a large hierarchy of signal elements, consider using the function Simulink.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.

  1. In the Bus Editor, create the Bus objects MyData and PressureBus.

  2. In the hierarchy pane, select the bus element pressure. Set the Dimensions property to [1 3].

  3. 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, 0.

    initStruct=Simulink.Bus.createMATLABStruct('MyData',[],[1 4]);

  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 and pressure.

  5. 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 fields s1 and s2.

  6. 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 substructure pressure in the third structure of initStruct, use this code:

    initStruct(3).pressure(2).s1 = 15.35;

Code Generation

Code generation for arrays of buses produces structures with a specific format. See Code Generation for Arrays of Buses.

Related Topics