Simulink.connectBlocks
Syntax
Description
connects the source specified by connection
= Simulink.connectBlocks(src
,dst
)src
to the destination specified by
dst
with signal lines. The source and destination can be on different
levels of the model hierarchy. When you programmatically connect blocks inside a
Variant Subsystem block, the connection is only made in the active
variant.
When the model is open, you can undo the connection by pressing Ctrl+Z (on macOS, press command+Z).
makes the connection and arranges the signals according to the routing style you specify as
a value for connection
= Simulink.connectBlocks(src
,dst
,RoutingStyle
=Value)RoutingStyle
.
Examples
Connect Blocks Without Specifying Ports
Open the model named ConnectBlocksWithoutSpecifyingPorts.slx
by entering this command in the MATLAB® Command Window. The model contains an unconnected Sine Wave block named Input Signal
and an unconnected Subsystem block named Plant
. The subsystem contains an unconnected Scope block named Scope1
.
model = "ConnectBlocksWithoutSpecifyingPorts";
open_system(model);
Connect the blocks named Input Signal
and Scope1
.
1. Specify the block paths of the source and destination blocks.
src = model+"/Input Signal"; dst = model+"/Plant/Scope1";
Alternatively, specify which blocks to connect using block handles.
srcPath = model+"/Input Signal"; dstPath = model+"/Plant/Scope1"; src = getSimulinkBlockHandle(srcPath); dst = getSimulinkBlockHandle(dstPath);
2. Connect the blocks.
connection = Simulink.connectBlocks(src,dst);
Connect Specific Block Ports
Open the model named ConnectSpecificBlockPorts.slx
by entering this command in the MATLAB® Command Window. The model includes a Bus Selector block named Bus Selector
with two unconnected output ports and an unconnected Subsystem block named Plant
. The Subsystem block named Plant
contains an unconnected Scope block named Scope1
with three unconnected input ports.
model = "ConnectSpecificBlockPorts";
open_system(model);
If you connect the block named Bus Selector
to the block named Scope1
using the Simulink.connectBlocks
function and you only specify which blocks to connect, not which ports, the function connects the top output port of the block named Bus Selector
to the top input port of the block named Scope1
. To connect the two blocks at different ports, you must specify which ports you want to connect. In this example, you connect the bottom port of the block named Bus Selector
to the bottom port of the block named Scope1
.
1. Specify the port paths of the source and destination blocks.
src = model+"/Bus Selector/2"; dst = model+"/Plant/Scope1/3";
Alternatively, specify which ports to connect using port handles.
srcBlockPath = model+"/Bus Selector"; srcBlockPortHandles = get_param(srcBlockPath,"PortHandles"); src = srcBlockPortHandles.Outport(2); dstBlockPath = model+"/Plant/Scope1"; dstBlockPortHandles = get_param(dstBlockPath,"PortHandles"); dst = dstBlockPortHandles.Inport(3);
2. Connect the blocks.
connection = Simulink.connectBlocks(src,dst);
Connect to Control Input Port of Conditionally Executed Subsystem
Open the model named ConnectToControlInputPort.slx
by entering this command in the MATLAB® Command Window. The model contains an unconnected Constant block and an unconnected Triggered Subsystem block.
model = "ConnectToControlInputPort";
open_system(model);
Connect the Constant block to the trigger port of the Triggered Subsystem block.
1. Specify the port paths of the source and destination blocks. Instead of ending with a port number, the path of the trigger port ends with the port name, Trigger
.
src = model+"/Constant/1"; dst = model+"/Triggered Subsystem/Trigger";
Alternatively, specify which ports to connect using port handles.
srcBlockPath = model+"/Constant/1"; srcBlockPortHandles = get_param(srcBlockPath,"PortHandles"); src = srcBlockPortHandles.Outport(1); blockPath = model+"/Triggered Subsystem"; blockPortHandles = get_param(blockPath,"PortHandles"); dst = blockPortHandles.Trigger;
2. Connect the blocks.
connection = Simulink.connectBlocks(src,dst);
Connect Blocks with Specific Routing Style
Open the model named ConnectBlocksWithSpecificRoutingStyle.slx
by entering this command in the MATLAB® Command Window. The model contains an unconnected Sine Wave block named Input Signal
and an unconnected Subsystem block named Plant
. The subsystem contains an unconnected Scope block named Scope1
.
model = "ConnectBlocksWithSpecificRoutingStyle";
open_system(model);
Connect the blocks named Input Signal
and Scope1
with smart automatic line routing turned off.
1. Specify the source and destination of the connection.
src = model+"/Input Signal"; dst = model+"/Plant/Scope1";
2. Connect the blocks. Set the value of RoutingStyle
to Simulink.Connection.RoutingStyle.Direct
.
connection = Simulink.connectBlocks(src,dst,...
RoutingStyle=Simulink.Connection.RoutingStyle.Direct);
Get Types, Handles, and Paths of Model Elements Created by Simulink.connectBlocks
Function
Open the model named GetNameHandleTypeofConnection.slx
by entering this command in the MATLAB® Command Window. The model contains an unconnected Sine Wave block named Input Signal
and an unconnected Subsystem block named Plant
. The subsystem contains an unconnected Scope block named Scope1
.
model = "GetNameHandleTypeofConnection";
open_system(model);
Connect the blocks named Input Signal
and Scope1
using the Simulink.connectBlocks
function.
src = "GetNameHandleTypeofConnection/Input Signal"; dst = "GetNameHandleTypeofConnection/Plant/Scope1"; connection = Simulink.connectBlocks(src,dst);
connection
is a Simulink.connectBlocks
object that contains an array of objects representing the model elements the Simulink.connectBlocks
function created to connect the blocks.
Get the array of objects.
transits = connection.getTransits;
Get Type, Handle, and Path of One Model Element
To get the type or handle of a model element the Simulink.connectBlocks
function created when making a connection, use dot notation. If the model element is a block, you can also get the block path using dot notation.
For example, to get the type of the model element corresponding to the object in the transits
array with index 1, use this command.
type = transits(1).getType
type = Type enumeration block
To get the handle, use this command.
handle = transits(1).getHandle
handle = 184.0016
To get the block path, use this command.
path = transits(1).getName
path = 'GetNameHandleTypeofConnection/Plant/Inport'
Get Types and Handles of All Model Elements
To get the types or handles of all model elements the Simulink.connectBlocks
function created when making a connection, you can use the arrayfun
function.
For example, to get the types, use these commands.
1. Create this function handle. The function input is represented by the variable t
. The handle defines a function that gets the object stored in the transits
array at index t
, and then gets the model element type for that object.
hFunc1 = @(t) transits(t).getType;
2. Get the indices of all the transits
array elements.
i = 1:numel(transits);
3. Pass the function handle and the indices to the arrayfun
function.
types = arrayfun(hFunc1,i)
types = 1x3 Type enumeration array block line line
The output is an enumeration array. To convert the enumeration array to a string array, use this command.
typesStr = string(types)
typesStr = 1x3 string
"block" "line" "line"
To get the handles, use these commands.
hFunc3 = @(t) transits(t).getHandle; i = 1:numel(transits); handles = arrayfun(hFunc3,i,UniformOutput=false)
handles=1×3 cell array
{[184.0016]} {[187.0026]} {[188.0024]}
Get Handles of Model Elements of Specific Type
You can also use the arrayfun
function to get the handles of all model elements of a specific type created by the Simulink.connectBlocks
function when making a connection.
For example, get the handles of all the signal lines represented by the objects in the transits
array.
1. Create this function handle. The handle defines the same function as before, but also compares the model element type of the array object to "line"
. If the model element type is a line, the function outputs 1
. Otherwise, the function outputs 0
.
hFunc1 = @(t) transits(t).getType == "line";
2. Create a second function handle. This handle defines a function that gets the model element handles.
hFunc2 = @(t) transits(t).getHandle;
3. Get the indices of the transits
array elements.
i = 1:numel(transits);
4. Pass the function handle named hFunc1
and the indices to the arrayfun
function. The output is a logical array that indicates which model elements in the transits
array are lines.
isTypeLine = arrayfun(hFunc1,i);
5. Get the handles of all model elements in the transits
array by passing the function handle named hFunc2
to the arrayfun
function.
h = arrayfun(hFunc2,i);
6. Get the handles of all signal lines by extracting the elements of the vector h
that correspond to signal lines. If an element in the vector h
corresponds to a signal line, the element of the vector isTypeLine
with the same index has a value of 1
.
h = h(isTypeLine == 1)
h = 1×2
187.0026 188.0024
Get Block Paths
You can use a similar approach to get the block paths of all blocks created by the Simulink.connectBlocks
function when making a connection.
1. Create this function handle. The function input is represented by the variable t
. The handle defines a function that gets the object stored in the transits
array at index t
, and then gets the model element type for that object. The function then compares the model element type of the array object to "block"
. If the model element type is a block, the function outputs 1
. Otherwise, the function outputs 0
.
hFunc1 = @(t) transits(t).getType == "block";
2. Create a second function handle. This handle defines a function that gets the model element paths.
hFunc2 = @(t) transits(t).getName;
3. Get the indices of the transits
array elements.
i = 1:numel(transits);
4. Pass the function handle named hFunc1
and the indices to the arrayfun
function. The output is a logical array that indicates which model elements in the transits
array are blocks.
isTypeBlock = arrayfun(hFunc1,i);
5. Get the block paths entries of all objects in the transits
array by passing the function handle named hFunc2
to the arrayfun
function. For objects corresponding to model elements that are not blocks, the entry is empty. To enable the output of a cell array that might contain both block paths and empty cell array elements, set UniformOutput
to false
.
h = arrayfun(hFunc2,i,UniformOutput=false)
h = 1x3 cell
{'GetNameHandleTypeofConnection/Plant/Inport'} {0x0 char} {0x0 char}
6. Get the block paths of all blocks by extracting the elements of the vector h
that correspond to signal lines. If an element in the vector h
corresponds to a signal line, the element of the vector isTypeLine
with the same index has a value of 1
.
h = h(isTypeBlock == 1)
h = 1x1 cell array
{'GetNameHandleTypeofConnection/Plant/Inport'}
Input Arguments
src
— Source of signal line
port handle | port path | block handle | block path
Source of the signal line you want to create, specified as a block or output port using a handle or path. If the specified destination is a block, the specified source must be a block. If the specified destination is a port, the specified source must be a port. If you specify the source and destination as blocks, the function connects an unconnected port on the source block to an unconnected port on the destination block. If the source you specify (block or port) is fully connected, the function branches one of the signal lines connected to the source.
For information about how to get the handle or path of a block or port, see Get Handles and Paths. Specify block and
port handles as scalars. Specify block and port paths as strings or character vectors.
Typically, the port path is the block path followed by a slash and the port number, for
example, "myModel/mysubSystem/myBlock/1"
. Using a port path to
specify a subsystem port whose name has been changed from the default port number is not
supported. Use a handle instead.
Example: "myModel/myBlock"
Example: "myModel/mysubSystem/myBlock/1"
dst
— Destination of signal line
port handle | port path | block handle | block path
Destination of the signal line you want to create, specified as a block or input port using a handle or path. If the specified source is a block, the specified destination must be a block. If the specified source is a port, the specified destination must be a port. If you specify the source and destination as blocks, the function connects an unconnected port on the source block to an unconnected port on the destination block. If the specified destination is a block, the block must have at least one unconnected port. If the specified destination is a port, the port must be unconnected.
For information about how to get the handle or path of a block or port, see Get Handles and Paths. Specify block and
port handles as scalars. Specify block and port paths as strings or character vectors.
Typically, the port path is the block path followed by a slash and the port number, for
example, "myModel/mysubSystem/myBlock/1"
. Using a port path to
specify a subsystem port whose name has been changed from the default port number is not
supported. Use a handle instead.
If the destination is the control input port of a conditionally executed subsystem,
the port path ends with the port name of the port instead of a port number, for example,
"myModel/mysubSystem/myBlock/Trigger"
. The table lists the port
names of the control input ports of conditionally executed subsystems.
Conditionally Executed Subsystem | Port Name |
---|---|
Triggered | Trigger |
Enabled | Enable |
Enabled and Triggered |
|
Resettable | Reset |
If Action | Ifaction |
Switch Case Action | Ifaction |
Function-Call | Trigger |
Message Polling | Trigger |
Message Triggered | Trigger |
The initial condition (IC) port of a While Iterator block has a port number, but the port number is not visible on the block. By default, the port number is 2.
Example: "myModel/myBlock"
Example: "myModel/mysubSystem/myBlock/1"
Example: "myModel/mysubSystem/myBlock/Trigger"
RoutingStyle
— Style of signal line layout
Simulink.Connection.RoutingStyle.Orthogonal
(default) | Simulink.Connection.RoutingStyle.Direct
Style of signal line layout, specified as
Simulink.Connection.RoutingStyle.Orthogonal
or
Simulink.Connection.RoutingStyle.Direct
. By default, the
Simulink.connectBlocks
function uses smart automatic line
routing. The signal lines the function creates are composed exclusively of orthogonal
line segments and avoid overlapping other blocks and signal lines.
To turn smart automatic line routing off and create signal lines that connect blocks
using the most direct route without avoiding overlap, specify the input argument
RoutingStyle
with the value
Simulink.Connection.RoutingStyle.Direct
.
To turn smart automatic line routing on, specify the input argument
RoutingStyle
with the value
Simulink.Connection.RoutingStyle.Orthogonal
.
Example: RoutingStyle=Simulink.Connection.RoutingStyle.Direct
Output Arguments
connection
— Model elements function creates to connect blocks
Simulink.connectBlocks
object
Model elements function creates to connect blocks, returned as a
Simulink.connectBlocks
object that contains an array of objects
corresponding to the model elements. The model elements can be signal lines and blocks.
For example, when connecting a block at the top level of the model to a block in a
subsystem, the function creates an Inport block in the subsystem.
To get the array of objects from the Simulink.connectBlocks
object,
use this command.
transit = connection.getTransit
Use these commands to get the type and handle of the model element corresponding to
the object in the transit
array with index i
. If
the model element is a block, you can also get the block path.
Attribute | Command |
---|---|
Type |
t(i).getType |
Handle |
t(i).getHandle |
Block path |
t(i).getName |
Version History
Introduced in R2024b
See Also
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)