Main Content

Programmatically Create Bus Element Ports

This example shows how to create bus element ports by adding In Bus Element and Out Bus Element blocks with the add_block function. To define the hierarchy of an input bus without adding blocks, this examples uses the Simulink.Bus.addElementToPort function. To get and set the attribute values for an element of a bus element port, this example uses the get_param and set_param functions.

Open a new model.

model = new_system("myModel");
open_system(model);

Programmatically Create Input Bus Element Ports

Add an In Bus Element block to the model.

add_block('simulink/Ports & Subsystems/In Bus Element',...
    'myModel/In Bus Element');

Default In Bus Element block labeled InBus.signal1

When you do not specify the port or element name, the new block uses the default names. By default, the input bus element port is named InBus, and the bus element is named signal1.

Add another In Bus Element block to the model. Instead of specifying a new block name, have the function make the block name unique with the MakeNameUnique argument. To avoid overlapping blocks, use the Position argument.

add_block('simulink/Ports & Subsystems/In Bus Element',...
    'myModel/In Bus Element',...
    'MakeNameUnique','on',...
    'Position','[230 35 240 45]');

Duplicate default In Bus Element blocks

The new block duplicates the previous block. Both blocks use the same default names.

Add a block for an element of the port named chirp, and replace the default port name with a custom name, such as Input1. Optionally, specify additional block parameters, such as background color, and element attributes, such as data type.

add_block('simulink/Ports & Subsystems/In Bus Element',...
    'myModel/In Bus Element', ...
    'MakeNameUnique','on',...
    'Position','[230 65 240 75]', ...
    'PortName','Input1', ...
    'Element','chirp',...
    'BackgroundColor','cyan',...
    'OutDataTypeStr','int32');

Three In Bus Element blocks for Input1 port. The new block is labeled Input1.chirp and has a cyan background color.

When you programmatically add an In Bus Element block with a nondefault port name, In Bus Element blocks that use the default port name update to use the nondefault port name.

To add an In Bus Element block for an existing port, copy the corresponding In Bus Element block from your model. For example, create a block that selects an element named sine from the port you previously created.

add_block('myModel/In Bus Element',...
    'myModel/In Bus Element',...
    'MakeNameUnique','on',...
    'Position','[230 95 240 105]', ...
    'Element','sine',...
    'BackgroundColor','magenta');

Four In Bus Element blocks for Input1 port

To add an In Bus Element block for a new port, add the In Bus Element block from the Simulink® library and specify the new port name. For example, create a block that selects an element named pulse from a port named Input2.

add_block('simulink/Ports & Subsystems/In Bus Element',...
    'myModel/In Bus Element',...
    'MakeNameUnique','on',...
    'Position','[230 125 240 135]', ...
    'PortName','Input2',...
    'Element','pulse')

Four In Bus Element blocks for Input1 port and one In Bus Element block for Input2 port

Programmatically Define Input Bus Hierarchy

To view the bus hierarchy at the port named Input2, double-click the block labeled Input2.pulse.

Dialog box for Input2 port

The dialog box displays the hierarchy of the input bus. The input bus contains only the element named pulse.

Add elements to the input bus.

Simulink.Bus.addElementToPort("myModel",...
    "Input2",...
    "saw");
Simulink.Bus.addElementToPort("myModel",...
    "Input2",...
    "constant.pi");
Simulink.Bus.addElementToPort("myModel",...
    "Input2",...
    "constant.one");
Simulink.Bus.addElementToPort("myModel",...
    "Input2",...
    "constant.zero");

In the dialog box, click Refresh.

Refresh button in dialog box

The dialog box updates to include the new elements. The top-level bus contains signals named pulse and saw and a nested bus named constant. The nested bus is collapsed.

Refreshed dialog box with nested bus collapsed

To expand the nested bus, click the arrow next to the bus name.

Refreshed dialog box with nested bus expanded

The nested bus contains signals named pi, one, and zero.

Programmatically Create Output Bus Element Ports

Add an Out Bus Element block to the model.

add_block('simulink/Ports & Subsystems/Out Bus Element',...
    'myModel/Out Bus Element',...
    'Position','[290 5 300 15]');

Default Out Bus Element block labeled OutBus.signal1

When you do not specify the port or element name, the new block uses the default names. By default, the output bus element port is named OutBus, and the bus element is named signal1.

Add another Out Bus Element block to the model.

add_block('simulink/Ports & Subsystems/Out Bus Element',...
    'myModel/Out Bus Element',...
    'MakeNameUnique','on',...
    'Position','[290 35 300 45]');

Two default Out Bus Element blocks with unique labels: OutBus.signal1 and OutBus.signal2

The new block adds an element to the output port. Both blocks use the same default port name, but the new block increments the element name to avoid a conflict.

Add a block for an element of the port named chirp, and replace the default port name with a custom name, such as Output1. Optionally, specify additional block parameters, such as background color, and element attributes, such as data type.

add_block('simulink/Ports & Subsystems/Out Bus Element',...
    'myModel/Out Bus Element',...
    'MakeNameUnique','on',...
    'Position','[290 65 300 75]', ...
    'PortName','Output1', ...
    'Element','chirp',...
    'BackgroundColor','green',...
    'OutDataTypeStr','int32');

Three Out Bus Element blocks for Output1 port. The new block is labeled Output1.chirp and has a green background color.

When you programmatically add an Out Bus Element block with a nondefault port name, Out Bus Element blocks that use the default port name update to use the nondefault port name.

To add an Out Bus Element block for an existing port, copy the corresponding Out Bus Element block from your model. For example, create a block that outputs an element named sine to the port you previously created.

add_block('myModel/Out Bus Element',...
    'myModel/Out Bus Element',...
    'MakeNameUnique','on',...
    'Position','[290 95 300 105]', ...
    'Element','sine',...
    'BackgroundColor','yellow');

Four Out Bus Element blocks for Output1 port

To add an Out Bus Element block for a new port, add the Out Bus Element block from the Simulink library and specify the new port name. For example, create a block that outputs an element named pulse to a port named Output2.

add_block('simulink/Ports & Subsystems/Out Bus Element',...
    'myModel/Out Bus Element',...
    'MakeNameUnique','on',...
    'Position','[290 125 300 135]', ...
    'PortName','Output2',...
    'Element','pulse')

Four Out Bus Element blocks for Output1 port and one Out Bus Element block for Output2 port

Programmatically Change Port Parameters

When you change a port parameter, the change applies to all blocks that correspond to the port.

For example, rename the first output port from Output1 to Out1 with the set_param function.

set_param('myModel/Out Bus Element','PortName','Out1')

Four Out Bus Element blocks with the port renamed to Out1

The labels of the Out Bus Element blocks that correspond with the port update to say Out1 instead of Output1.

The new port name must be unique within the model component.

Programmatically Change Block Parameters

When you change a block parameter, the change applies to only the specified block.

For example, change the background color from black to orange for the first In Bus Element block you added by using the set_param function.

set_param('myModel/In Bus Element','BackgroundColor','orange')

The first In Bus Element block now has an orange background color.

Programmatically Change Element Attributes

By specifying a combination of the model component and the element label, you can change the value of an attribute of an existing top-level bus, nested bus, signal, or message at a bus element port.

For example, set the minimum and maximum values of the element Input1.sine to -1 and 1, respectively, with the get_param function.

set_param('myModel/Out1.sine','OutMin','-1','OutMax','1')

To check the value of each attribute, use the get_param function.

get_param('myModel/Out1.sine','OutMin')
ans = 
'-1'
get_param('myModel/Out1.sine','OutMax')
ans = 
'1'

See Also

Blocks

Functions

Related Topics

External Websites