Create Bus Objects Programmatically
你can programmatically create aSimulink.Bus
object and itsSimulink.BusElement
objects from arrays, blocks, cell arrays, structures, or C code.
As you createBus
objects programmatically, you can store them in the MATLAB®workspace or a data dictionary or save their definitions in a function. ForBus
objects in the base workspace, you can programmatically save their definitions in a function using theSimulink.Bus.save
function.
To simulate a block that uses aBus
object, thatBus
object must be in the base workspace or in a data dictionary.
Create Bus Objects from Arrays
创建一个hierarchy ofBus
objects using arrays. Array indexing lets you create and access multiple elements in an array. Dot notation lets you access property values.
创建twoBusElement
objects, namedChirp
和Sine
, in the base workspace.
elems(1) = Simulink.BusElement; elems(1).Name ='Chirp'; elems(2) = Simulink.BusElement; elems(2).Name =的正弦;
创建一个Bus
object, namedNestedBus
, that uses the elements defined in theelems
array.
NestedBus = Simulink.Bus; NestedBus.Elements = elems;
创建two moreBusElement
objects, namedNestedBus
和Step
. To haveNestedBus
represent aBus
object, specify aBus
object 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 theelems
array.
TopBus = Simulink.Bus; TopBus.Elements = elems;
你can view the created objects in the公共汽车编辑器.
buseditor
Create Bus Objects from Blocks
To programmatically create aBus
object based on a block in a model, use theSimulink.Bus.createObject
function.
If you specify aBus Creatorblock that is at the highest level of a bus hierarchy, the function createsBus
objects for all of the buses in the hierarchy, including nested buses.
Create Bus Objects fromMATLABData
To create aBus
object from a cell array, use theSimulink.Bus.cellToObject
function. Each subordinate cell array represents aBus
object
To create aBus
object from a MATLAB structure, use theSimulink.Bus.createObject
function. The structure can contain MATLABtimeseries
, MATLABtimetable
, andmatlab.io.datastore.SimulationDatastore
objects or be a numeric structure.
Create Bus Objects from External C Code
你can create aBus
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 theBus
object to package signal or parameter data according to the structure type. To create the object, use theSimulink.importExternalCTypes
function.