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 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.
open_system(new_system);
Programmatically Create Input Bus Element Ports
Add an In Bus Element block to the model.
add_block('simulink/Ports & Subsystems/In Bus Element',... [gcs '/In Bus Element']);
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',... [gcs '/In Bus Element'],... 'MakeNameUnique','on',... 'Position','[230 35 240 45]');
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',... [gcs '/In Bus Element'], ... 'MakeNameUnique','on',... 'Position','[230 65 240 75]', ... 'PortName','Input1', ... 'Element','chirp',... 'BackgroundColor','cyan',... 'OutDataTypeStr','int32');
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([gcs '/In Bus Element'],... [gcs '/In Bus Element'],... 'MakeNameUnique','on',... 'Position','[230 95 240 105]', ... 'Element','sine',... 'BackgroundColor','magenta');
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',... [gcs '/In Bus Element'],... 'MakeNameUnique','on',... 'Position','[230 125 240 135]', ... 'PortName','Input2',... 'Element','pulse')
Programmatically Create Output Bus Element Ports
Add an Out Bus Element block to the model.
add_block('simulink/Ports & Subsystems/Out Bus Element',... [gcs '/Out Bus Element'],... 'Position','[290 5 300 15]');
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',... [gcs '/Out Bus Element'],... 'MakeNameUnique','on',... 'Position','[290 35 300 45]');
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',... [gcs '/Out Bus Element'],... 'MakeNameUnique','on',... 'Position','[290 65 300 75]', ... 'PortName','Output1', ... 'Element','chirp',... 'BackgroundColor','green',... 'OutDataTypeStr','int32');
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([gcs '/Out Bus Element'],... [gcs '/Out Bus Element'],... 'MakeNameUnique','on',... 'Position','[290 95 300 105]', ... 'Element','sine',... 'BackgroundColor','yellow');
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',... [gcs '/Out Bus Element'],... 'MakeNameUnique','on',... 'Position','[290 125 300 135]', ... 'PortName','Output2',... 'Element','pulse')
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([gcs '/Out Bus Element'],'PortName','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([gcs '/In Bus Element'],'BackgroundColor','orange')
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([gcs '/Out1.sine'],'OutMin','-1','OutMax','1')
To check the value of each attribute, use the get_param
function.
get_param([gcs '/Out1.sine'],'OutMin')
ans = '-1'
get_param([gcs '/Out1.sine'],'OutMax')
ans = '1'