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
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);
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);
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);
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);
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 creates 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 creates 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 block handle, use this command.
handle = transits(1).getHandle
handle = 282.0054
To get the block path, use this command.
path = transits(1).getName
path = 'GetNameHandleTypeofConnection/Plant/Input Signal'
Get Types and Handles of All Model Elements
To get the types of all model elements the Simulink.connectBlocks
function creates when making a connection, use this command.
types = arrayfun(@(x)x.getType,transits)
types = 3×1 Type enumeration array block line line
The arrayfun
function executes the command specified in the first input argument for every element of the array specified in the second input argument. In the command, the array element is represented by the variable specified in the format @(name)
. In this example, the array element is represented by the variable x
. For each transits
array element, x
, the arrayfun
function executes x.getType()
. That is, for every object in the transits
array, the arrayfun
function outputs the object type.
To get the handles of all model elements the Simulink.connectBlocks
function created when making a connection, use this command.
handles = arrayfun(@(x)x.getHandle,transits)
handles = 3×1
282.0054
285.0067
286.0079
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 the Simulink.connectBlocks
function creates when making a connection.
For example, get the handles of all the signal lines represented by the objects in the transits
array.
First, determine which objects in the transits
array are signal lines using this command.
isLine = arrayfun(@(x)x.getType=="line",transits)
isLine = 3×1 logical array
0
1
1
The arrayfun
function executes the command specified in the first input argument for every element of the array specified in the second input argument. In the command, the array element is represented by the variable specified in the format @(name)
. In this example, the array element is represented by the variable x
. For each transits
array element, x
, the arrayfun
function executes x.getType()=="line"
. That is, for every object in the transits
array, the arrayfun
function outputs 1
if the object represents a signal line or 0
if the object does not represent a signal line.
Store the objects in the transits
array that represent signal lines in an array.
lineTransits = transits(isLine);
Get the handles of the objects in the transits
array that represent signal lines.
h = arrayfun(@(x)x.getHandle,lineTransits)
h = 2×1
285.0067
286.0079
For each lineTransits
element, x
, the arrayfun
function executes x.getHandle
. That is, for every object in the lineTransits
array, the arrayfun
function outputs the handle.
Get Block Paths
You can use a similar approach to get the block paths of all blocks the Simulink.connectBlocks
function creates when making a connection.
isBlock = arrayfun(@(x)x.getType=="block",transits);
blockTransits = transits(isBlock);
paths = arrayfun(@(x)x.getName,blockTransits,UniformOutput=false)
paths = 1×1 cell array
{'GetNameHandleTypeofConnection/Plant/Input Signal'}
The output of the last command might be nonscalar. Set UniformOutput
to false
to support nonscalar outputs.
To convert the output to a string array, use this command.
paths = string(paths)
paths = "GetNameHandleTypeofConnection/Plant/Input Signal"
Input Arguments
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.
If the source is the state port of an Integrator block, the port path
ends with the port name State
instead of a port number, for example,
"myModel/mysubSystem/myBlock/State"
.
To connect to a state port, the state port must be visible. By default, the state
port on the Integrator block is hidden. To make the port visible, run
this command, where path
is the block path of the
Integrator block.
set_param(path,ShowStatePort="on")
Example: "myModel/myBlock"
Example: "myModel/mysubSystem/myBlock/1"
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"
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
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 R2024bStarting in R2025a, you can get a block or port path from the context menu. Right-click the block or port and select Copy Path. When you paste the path into a command, remember to add quotes to convert the path into a string or a character vector. For more information about getting and using paths, see Get Handles and Paths.
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: United States.
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)