Main Content

Programmatically Create Simulink Bus Objects

You can programmatically create a Simulink.Bus object and its Simulink.BusElement objects from arrays, blocks, cell arrays, structures, or C code.

As you create bus objects programmatically, you can store them in the MATLAB® workspace or a data dictionary or save their definitions in a function. For bus objects in the base workspace, you can programmatically save their definitions in a function using the Simulink.Bus.save function.

To simulate a block that uses a bus object, that bus object must be in the base workspace or in a data dictionary.

Create Bus Objects from Bus Element Objects

Create a hierarchy of Simulink.Bus objects using arrays of Simulink.BusElement objects.

Create an array that contains two BusElement objects, named Chirp and Sine, in the base workspace.

elems(1) = Simulink.BusElement;
elems(1).Name = 'Chirp';

elems(2) = Simulink.BusElement;
elems(2).Name = 'Sine';

Array indexing lets you create and access the elements of the array. Dot notation lets you access property values of the elements.

Create a Bus object, named Sinusoidal, that contains the elements defined in the elems array.

Sinusoidal = Simulink.Bus;
Sinusoidal.Elements = elems;

To create a hierarchy of Bus objects, create another Bus object to reference the Bus object named Sinusoidal.

Create an array that contains two BusElement objects, named NestedBus and Step. Specify the Bus object named Sinusoidal as the data type of the NestedBus element.

clear elems

elems(1) = Simulink.BusElement;
elems(1).Name = 'NestedBus';
elems(1).DataType = 'Bus: Sinusoidal';

elems(2) = Simulink.BusElement;
elems(2).Name = 'Step';

Create a Bus object, named TopBus, that contains the elements defined in the elems array.

TopBus = Simulink.Bus;
TopBus.Elements = elems;

You can view the hierarchy of the created objects in the Type Editor.

typeeditor

Create Bus Objects from Blocks

To programmatically create a Simulink.Bus object based on a block in a model, use the Simulink.Bus.createObject function.

If you specify a Bus Creator block that is at the highest level of a bus hierarchy, the function creates bus objects for all of the buses in the hierarchy, including nested buses.

Create Bus Objects from MATLAB Data

To create a Simulink.Bus object from a cell array, use the Simulink.Bus.cellToObject function. Each subordinate cell array represents another bus object.

To create a bus object from a MATLAB structure, use the Simulink.Bus.createObject function. The structure can contain MATLAB timeseries, MATLAB timetable, and matlab.io.datastore.SimulationDatastore objects or be a numeric structure.

Create Bus Objects from External C Code

You can create a Simulink.Bus object that corresponds to a structure type (struct) that your existing C code defines. Then, in preparation for integrating existing algorithmic C code for simulation (for example, by using the Legacy Code Tool), you can use the bus object to package signal or parameter data according to the structure type. To create the object, use the Simulink.importExternalCTypes function.

See Also

Functions

Classes

Related Topics