Main Content

Create Bus Objects Programmatically

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 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 Arrays

Create a hierarchy of Bus objects using arrays. Array indexing lets you create and access multiple elements in an array. Dot notation lets you access property values.

Create 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';

Create a Bus object, named NestedBus, that uses the elements defined in the elems array.

NestedBus = Simulink.Bus;
NestedBus.Elements = elems;

Create two more BusElement objects, named NestedBus and Step. To have NestedBus represent a Bus object, specify a Bus object data type.

clear elems

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

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

Create the bus at the top of the bus hierarchy that uses the elements defined in the elems array.

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

You can view the created objects in the Bus Editor.


Create Bus Objects from Blocks

To programmatically create a 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 Bus object from a cell array, use the Simulink.Bus.cellToObject function. Each subordinate cell array represents a 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 objects or be a numeric structure.

Create Bus Objects from External C Code

You can create a 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



Related Topics