To create a bus object and its bus elements programmatically, use one of these approaches:
Create Simulink.Bus and Simulink.BusElement Objects Directly —
Simulink.Bus object and define its
for the elements of the bus.
Create Bus Objects from Blocks — Create a bus object and its bus element objects based on a block in a model.
Create Bus Objects from MATLAB Data — Create a bus object and its bus elements based on a MATLAB® structure or cell array.
As you create bus objects programmatically, you can store them in either the MATLAB workspace or in a MATLAB code file. After you create a bus object, you can use MATLAB commands to save the bus objects to a MAT-file (see Save and Load Workspace Variables). To simulate a block that uses a bus object, that bus object must be in the base workspace or in a data dictionary.
You can specify the bus object to be the data type of a block either before or after defining the bus object. However, before you simulate the model, the bus object and the corresponding bus signal must have the same number of bus elements, in the same order. Also, each bus element in the bus object and in the corresponding bus element signal must have the same data type and dimensions.
During model development, you can modify bus signals to match bus objects or modify bus objects to match buses.
You can create a
programmatically. The bus objects are stored in the base workspace.
For each bus element object, specify the name, dimensions, and data
type. The other bus element object properties are optional. For the
bus object, specify the bus elements. The other bus object properties
are optional. For example, this code creates two bus element objects
that are then used as the elements of the
clear elems; elems(1) = Simulink.BusElement; elems(1).Name = 'VALVE1'; elems(1).Dimensions = 1; elems(1).DimensionsMode = 'Fixed'; elems(1).DataType = 'double'; elems(1).SampleTime = -1; elems(1).Complexity = 'real'; elems(2) = Simulink.BusElement; elems(2).Name = 'VALVE2'; elems(2).Dimensions = 1; elems(2).DimensionsMode = 'Fixed'; elems(2).DataType = 'double'; elems(2).SampleTime = -1; elems(2).Complexity = 'real'; CONTROL = Simulink.Bus; CONTROL.Elements = elems;
This script is similar to the file you get if you save a bus
object to a MATLAB file and choose the
For information about saving bus objects, see Save and Import Bus Objects.
You can create a bus object and its bus elements programmatically
based on a block in a model. Use the
and specify the model and blocks for which to create bus objects for.
Before you use the function, the model must be compilable. For example,
f you specify a Bus Creator block that is at the highest
level of a nested bus hierarchy, the function creates bus objects
for all of the buses in the hierarchy. You can specify to have the
bus objects created in the base workspace or to save them in a MATLAB code
file. For an example, see Use Bus Creator Blocks to Create a Bus Object.
You can create a bus object from cell arrays of bus information
in MATLAB, using the
Each cell subordinate cell array represents a bus object and includes
the following data, reflecting
Elements field is a cell array defining
these properties for each
You can create a bus object from a MATLAB structure of
For an example, see Use a Structure of Timeseries Objects to Create a Bus Object.
Alternatively, you can specify a numeric MATLAB structure. You
can specify to have the bus objects created in the base workspace
or to save them in a MATLAB code file.