Programmatically Initialize Arrays of Buses
This example shows how to initialize an array of buses. Suppose that you define
Simulink.Bus
objects named MyData
and
PressureBus
.
% Bus object: MyData clear elems; elems(1) = Simulink.BusElement; elems(1).Name = 'temperature'; elems(1).DataType = 'int16'; elems(2) = Simulink.BusElement; elems(2).Name = 'pressure'; elems(2).DataType = 'Bus: PressureBus'; MyData = Simulink.Bus; MyData.Elements = elems; % Bus object: PressureBus clear elems; elems(1) = Simulink.BusElement; elems(1).Name = 's1'; elems(2) = Simulink.BusElement; elems(2).Name = 's2'; PressureBus = Simulink.Bus; PressureBus.Elements = elems; clear elems;
The object named MyData
has two elements, named
temperature
and pressure
. The
pressure
element uses Bus
object
PressureBus
, which has two elements, named s1
and
s2
.
The data type of temperature
is int16
, and the data
types of s1
and s2
are double
.
To specify initial conditions for an array of buses, 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 an array of
three buses. You can use initValues
to specify the Initial
condition parameter of a block such as a Unit Delay block.
Alternatively, you can use a 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 Simulink.Bus.createMATLABStruct
function.
Suppose pressure
represents a nested array of buses with dimensions
[1 3]
. To programmatically set this value, access the
Dimensions
property of the second element of the
Elements
property of the MyData
object.
MyData.Elements(2).Dimensions = '[1 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])
initStruct = 1×4 struct array with fields: temperature pressure
Inspect the created structure.
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 interactively specify the values using the variable editor.
Alternatively, you can programmatically specify the values. For example, to assign value
of 15.35
to the field s1
of the second substructure
pressure
in the third structure of initStruct
,
enter:
initStruct(3).pressure(2).s1 = 15.35;