Assign into Elements of Array of Buses
This example shows how to assign data for the buses in an array of buses. MATLAB Function blocks can directly assign the data for a bus in an array of buses. They can also make a partial assignment to certain elements in the array of buses.
Open and compile the example model, which contains a MATLAB Function block that receives a nonvirtual bus. To compile the model, on the Modeling tab of the Simulink® Toolstrip, click Update Model or Run. Compiling the model updates the line styles, which you can use to visually identify buses.
mdl = "ArrayOfBusesPartialAssignment"; open_system(mdl) set_param(mdl,SimulationCommand="Update");
The In Bus Element and Out Bus Element blocks specify a Simulink.Bus
object named A
. To view the hierarchy and specified attributes of the input bus, double-click the In Bus Element block.
The top-level bus is a nonvirtual bus defined by bus object A
. Element a
is a Boolean signal. Element a1
is an array of buses that contains ten nonvirtual buses defined by bus object B
. Each bus in the array of buses is composed of elements named b
and b1
.
The MATLAB Function block specifies bus object A
as the data type for the block input and output. To view this specification, double-click the MATLAB Function block. In the toolstrip, on the Function tab, click Ports & Data Manager.
The MATLAB Function block uses a function to iteratively assign new values to element b
for each bus in the array of buses. The function also assigns a new value to element a
.
function y = fcn(u) y = u; y.a = false; for idx = 1:length(y.a1) y.a1(idx).b = int32(zeros(5,1)); end