add_line
Add line to Simulink model
Syntax
Description
adds a line in the model or subsystem h
= add_line(sys
,out
,in
)sys
that connects one
block's output port out
to another block's input port
in
. This syntax draws the most direct route from port to
port, for example, diagonal lines or lines that go through other blocks.
You can connect ports when:
The input port does not already have a connection.
The ports are compatible for connecting.
The ports are either both at the top level of the model hierarchy or both in the same model component, for example, in the same subsystem. To connect blocks in different components, use the
Simulink.connectBlocks
function.
connects blocks, specifying whether to route the lines around other blocks.h
= add_line(sys
,out
,in
,'autorouting',autoOption
)
adds a line drawn by (x,y) coordinate h
= add_line(sys
,points
)points
relative to the
upper-left corner of the Simulink® Editor canvas before any canvas resizing. If either end of the line is
within five pixels of a corresponding port, the function connects the line to it.
The line can have multiple segments.
Examples
Create and open a new model.
open_system(new_system('myModel'));
Add and position a Constant block.
add_block('simulink/Commonly Used Blocks/Constant','myModel/Constant'); set_param('myModel/Constant','position',[140,80,180,120]);
Add and position a Gain block.
add_block('simulink/Commonly Used Blocks/Gain','myModel/Gain'); set_param('myModel/Gain','position',[220,80,260,120]);
Connect the output port of the Constant block to the input port of the Gain block. Since the Constant block only has one output port, the only port number you can specify for the Constant block is 1
. Since the Gain block only has one input port, the only port number you can specify for the Gain block is 1
.
add_line('myModel','Constant/1','Gain/1');
Add and position a Scope block.
add_block('simulink/Commonly Used Blocks/Scope','myModel/Scope'); set_param('myModel/Scope','position',[300,80,340,120]);
Change the number of input ports of the Scope block to 2
.
set_param('myModel/Scope','NumInputPorts','2')
Connect the output port of the Gain block to the second input port of the Scope block. Since the Gain block only has one output port, the only port number you can specify for the Gain block is 1
. Since you want to connect to the second port of the Scope block, for the Scope block, specify the port number 2
.
add_line('myModel','Gain/1','Scope/2');
Open the example. Then, open the vdp
model.
open_system('vdp')
Delete the line that connects the masked Subsystem block named Mu
to the Sum block.
delete_line('vdp','Mu/1','Sum/2');
Get the port handles from the masked Subsystem block named Mu
and the Sum block.
h1 = get_param('vdp/Mu','PortHandles'); h2 = get_param('vdp/Sum','PortHandles')
h2 = struct with fields:
Inport: [172.0023 173.0051]
Outport: 174.0079
Enable: []
Trigger: []
State: []
LConn: []
RConn: []
Ifaction: []
Reset: []
Event: []
In the h2
structure, note the two handles for the Inport
property.
Connect the Subsystem block named Mu
to the Gain block. Specify the ports to connect by indexing into the Inport
and Outport
properties of the port handles.
add_line('vdp',h1.Outport(1),h2.Inport(2));
You can programmatically make a connection that branches an existing signal line. You can use the points
syntax to draw the segment, or you can draw the line by specifying the ports to connect. When using the ports, use automatic line routing to improve the layout of the branched line.
This example shows how to make a connection with a branched signal line using ports.
Open the example. Then, open the vdp
model.
open_system('vdp')
Add a Scope block to the vdp
model.
add_block('simulink/Commonly Used Blocks/Scope','vdp/Scope1');
Position the Scope block above the Outport block.
set_param('vdp/Scope1','position',[470,70,500,110]);
Connect the Integrator block named x1
to the Scope block named Scope1
.
add_line('vdp','x1/1','Scope1/1','autorouting','on')
The command branches the existing line from the Integrator block named x1
and connects it to the Scope block named Scope1
. With autorouting on, the resulting line is segmented.
Open the example. Then, open the vdp
model.
open_system('vdp')
Delete the line that connects the masked Subsystem block named Mu
to the Sum block.
delete_line('vdp','Mu/1','Sum/2')
Get the port locations for the masked Subsystem block named Mu
. Mu
has two ports: one input port and one output port.
mu = get_param('vdp/Mu','PortConnectivity'); mu.Position
ans = 1×2
210 185
ans = 1×2
250 185
The first set of coordinates corresponds to the input block. The second set corresponds to the output block.
Get the port locations for the Sum block, which has three ports: two input ports and one output port.
s = get_param('vdp/Sum','PortConnectivity'); s.Position
ans = 1×2
310 170
ans = 1×2
310 185
ans = 1×2
345 180
The first set of coordinates corresponds to the upper input port, the second set corresponds to the lower input port, and the third corresponds to the output port.
To connect the masked Subsystem block named Mu
to the Sum block, specify the coordinates of the output port of the Subsystem block then the coordinates of the lower input port of the Sum block.
add_line('vdp',[250 150; 310 150])
You can add lines with and without autorouting options.
Add Blocks to Model
Create a new model named myModel
.
open_system(new_system('myModel'))
Turn the HideAutomaticNames
parameter off.
set_param('myModel','HideAutomaticNames','off')
Add two Subsystem blocks and a Gain block.
add_block('simulink/Commonly Used Blocks/Subsystem','myModel/Subsystem1'); add_block('simulink/Commonly Used Blocks/Subsystem','myModel/Subsystem2'); add_block('simulink/Commonly Used Blocks/Gain','myModel/Gain');
Position the blocks such that the Gain block is between the two Subsystem blocks.
set_param('myModel/Subsystem1','position',[100 100 130 130]); set_param('myModel/Gain','position',[230,100,260,130]); set_param('myModel/Subsystem2','position',[360,100,390,130]);
Add an additional input port and output port to each Subsystem block.
add_block('simulink/Sources/In1','myModel/Subsystem1/In2'); add_block('simulink/Commonly Used Blocks/Out1','myModel/Subsystem1/Out2'); add_block('simulink/Sources/In1','myModel/Subsystem2/In2'); add_block('simulink/Commonly Used Blocks/Out1','myModel/Subsystem2/Out2');
Connect Blocks Without Autorouting
Add lines to connect the output ports of the Subsystem block named Subsystem1
to the input ports of the Subsystem block named Subsystem2
.
add_line('myModel',{'Subsystem1/1','Subsystem1/2'},{'Subsystem2/1','Subsystem2/2'})
Connect Blocks with Autorouting
Delete the lines.
delete_line('myModel','Subsystem1/1','Subsystem2/1') delete_line('myModel','Subsystem1/2','Subsystem2/2')
Add the lines again using the smart
autorouting option. When you use an array to connect two sets of input and output ports, smart autorouting routes them together if doing so makes better use of the space.
add_line('myModel',{'Subsystem1/1','Subsystem1/2'},{'Subsystem2/1','Subsystem2/2'},'autorouting','smart')
Input Arguments
Model or subsystem to add the line to, specified as a character vector.
Example: 'vdp'
Example: 'f14/Controller'
Block output port to connect line from, specified as one of these values:
The name of the block to which the port attaches followed by a slash and the port number, for example,
"myBlock/1"
.For the state port on an Integrator block, use the port name
State
instead of a port number. 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, wherepath
is the block path of the Integrator block expressed as a string or character vector, for example,"myModel/myBlock"
.set_param(path,ShowStatePort="on")
The port handle.
An array of either of these port designators.
Use 'PortHandles'
with get_param
to get the handles.
Example: 'Mu/1'
Example: 'Subsystem/2'
Example: h.Outport(1)
Example: {'Subsystem/1','Subsystem/2'}
Tips
Most block ports are numbered from top to bottom or from left to right. For a description of the port order for various block orientations, see Identify Port Location on Rotated or Flipped Block.
Moving a port on a Subsystem block can change the port number. For more information, see Move Ports.
Block input port to connect line to, specified as one of these values:
The name of the block to which the port attaches followed by a slash and the port number, for example,
"myBlock/1"
. For the control input port of a conditionally executed subsystem, use the port name instead of the port number, for example,"myBlock/Reset"
. For the port names of other conditionally used subsystems, see Get Port Name.The port handle.
An array of either of these port designators.
Use the 'PortHandles'
option with
get_param
to get handles.
Example: 'Mu/1'
Example: 'Subsystem/2'
Example: h.Inport(1)
Example: {'Subsystem/1','Subsystem/2'}
Tips
Most block ports are numbered from top to bottom or from left to right. For a description of the port order for various block orientations, see Identify Port Location on Rotated or Flipped Block.
Moving a port on a Subsystem block can change the port number. For more information, see Move Ports.
Type of automatic line routing around other blocks, specified as:
'off'
for no automatic line routing'on'
for automatic line routing'smart'
for automatic line routing that takes the best advantage of the blank spaces on the canvas and avoids overlapping other lines and labels
Points of the line to draw, specified as at least a 2-by-2 matrix. Add a row for every segment you want to draw. Specify points as (x,y) coordinates from the upper-left corner of the Editor before any canvas resizing.
Example: [100 300; 200 300; 200 300; 200
500]
Output Arguments
Line created by add_line
, returned as a
handle.
Version History
Introduced before R2006a
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)