Create Bus Objects Programmatically

To create a Bus object and its bus elements programmatically from scratch or based on a block, data, or C code.

As you create Bus objects programmatically, you can store them in either the MATLAB workspace or in a function. 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 (MATLAB)). 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 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 buses to match Bus objects or modify Bus objects to match buses.

Create Simulink.Bus and Simulink.BusElement Objects Directly

You can create a Simulink.Bus object and its Simulink.BusElement objects 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 CONTROL Bus object.

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 function and choose the Object format. For information about saving Bus objects, see Save Simulink.Bus Objects.

Create Bus Objects from Blocks

You can create a Bus object and its bus elements programmatically based on a block in a model. Use the Simulink.Bus.createObject function 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, if 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 function.

Create Bus Objects from MATLAB Data

You can create a Bus object from cell arrays of bus information in MATLAB, using the Simulink.Bus.cellToObject function. Each cell subordinate cell array represents a Bus object and includes the following data, reflecting Simulink.Bus object properties:


The Elements field is a cell array defining these properties for each Simulink.BusElement object:


You can create a Bus object from a MATLAB structure of timeseries objects, using the Simulink.Bus.createObject function. 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 function.

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