Main Content

Create Bus Objects Programmatically

你can programmatically create aSimulink.Busobject and itsSimulink.BusElementobjects from arrays, blocks, cell arrays, structures, or C code.

As you createBusobjects programmatically, you can store them in the MATLAB®workspace or a data dictionary or save their definitions in a function. ForBusobjects in the base workspace, you can programmatically save their definitions in a function using theSimulink.Bus.savefunction.

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

Create Bus Objects from Arrays

创建一个hierarchy ofBusobjects using arrays. Array indexing lets you create and access multiple elements in an array. Dot notation lets you access property values.

创建twoBusElementobjects, namedChirpSine, in the base workspace.

elems(1) = Simulink.BusElement; elems(1).Name ='Chirp'; elems(2) = Simulink.BusElement; elems(2).Name =的正弦;

创建一个Busobject, namedNestedBus, that uses the elements defined in theelemsarray.

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

创建two moreBusElementobjects, namedNestedBusStep. To haveNestedBusrepresent aBusobject, specify aBusobject data type.

clearelemselems(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 theelemsarray.

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

你can view the created objects in the公共汽车编辑器.

buseditor

Create Bus Objects from Blocks

To programmatically create aBusobject based on a block in a model, use theSimulink.Bus.createObjectfunction.

If you specify aBus Creatorblock that is at the highest level of a bus hierarchy, the function createsBusobjects for all of the buses in the hierarchy, including nested buses.

Create Bus Objects fromMATLABData

To create aBusobject from a cell array, use theSimulink.Bus.cellToObjectfunction. Each subordinate cell array represents aBusobject

To create aBusobject from a MATLAB structure, use theSimulink.Bus.createObjectfunction. The structure can contain MATLABtimeseries, MATLABtimetable, andmatlab.io.datastore.SimulationDatastoreobjects or be a numeric structure.

Create Bus Objects from External C Code

你can create aBusobject 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 theBusobject to package signal or parameter data according to the structure type. To create the object, use theSimulink.importExternalCTypesfunction.

See Also

Functions

Classes

Related Topics