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.
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
Connect Blocks Using Port Numbers
Use the block port numbers to add a line to connect blocks.
Create and open a model.
open_system(new_system('connect_model'));
Add and position a Constant block and a Gain block.
add_block('simulink/Commonly Used Blocks/Constant','connect_model/Constant'); set_param('connect_model/Constant','position',[140,80,180,120]); add_block('simulink/Commonly Used Blocks/Gain','connect_model/Gain'); set_param('connect_model/Gain','position',[220,80,260,120]);
Connect the blocks. Specify port 1.
add_line('connect_model','Constant/1','Gain/1');
Connect Blocks Using Port Handles
Get the port handles and connect the ports using
add_line
.
Open the model vdp
.
open_system('vdp');
Delete the line that connects the Gain block named
Mu
to the Sum block.
delete_line('vdp','Mu/1','Sum/2');
Get the port handles from the Mu
block and the Sum
block.
h = get_param('vdp/Mu','PortHandles'); h1 = get_param('vdp/Sum','PortHandles');
Look at the h1
structure. Notice the two handles for
the Inport
property.
h1
h1 = struct with fields: Inport: [47.0002 54.0002] Outport: 39.0002 Enable: [] Trigger: [] State: [] LConn: [] RConn: [] Ifaction: [] Reset: []
Index into the Outport
and Inport
properties on the port handles to get the handles you want and connect them.
Connect to the second inport.
add_line('vdp',h.Outport(1),h1.Inport(2));
Add a Branched Line
You can branch a line by adding a connection
programmatically. 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 port, use automatic line routing to improve the look of the branched
line.
Add a scope to the vdp
model above the outport.
vdp add_block('simulink/Commonly Used Blocks/Scope','vdp/Scope1'); set_param('vdp/Scope1','position',[470,70,500,110]);
Connect the Integrator block x1
to
Scope1
. This code branches the existing line from the
x1
output and connects it to the scope. With
autorouting on, the resulting line is segmented.
add_line('vdp','x1/1','Scope1/1','autorouting','on')
Connect Blocks Using Points
You can use points on the canvas as the start and end of each
segment. Get the port locations using get_param
with the
'PortConnectivity'
option.
Open the model vdp
and delete the line that connects
the Mu
and Sum blocks.
vdp delete_line('vdp','Mu/1','Sum/2')
Get the port locations for Mu
. Mu
has two ports. The first is the input port, and the second is the output
port.
mu = get_param('vdp/Mu','PortConnectivity'); mu.Position
ans = 190 150 ans = 225 150
Get the port locations for Sum
, which has three ports.
The second position is the lower input port.
s = get_param('vdp/Sum','PortConnectivity'); s.Position
ans = 250 135 ans = 250 150 ans = 285 145
Connect the ports using the output and input points.
add_line('vdp',[225 150; 250 150])
Connect Blocks Using Autorouting Options
Add lines with and without autorouting options.
Create a model route
. Display default block
names.
open_system(new_system('route')); set_param('route','HideAutomaticNames','off')
Add two Subsystem blocks and a Gain block. Add an inport and outport to each Subsystem block.
Add lines to connect the outports of the Subsystem1
block to the inports of the Subsystem block.
add_line('route',{'Subsystem/1','Subsystem/2'},... {'Subsystem1/1','Subsystem1/2'})
Because you did not use the autorouting options, the function draws straight lines that cross over the Gain block.
Delete the lines. Add lines again, this time with the autorouting option
set to 'on'
.
add_line('route',{'Subsystem/1','Subsystem/2'},... {'Subsystem1/1','Subsystem1/2'},'autorouting','on')
The lines route around the Gain block.
Delete the lines. Add lines again, using the smart
autorouting option. When you use an array to connect two sets of inports and
outports, smart
autorouting routes them together if doing
so makes better use of the space.
add_line('route',{'Subsystem/1','Subsystem/2'},... {'Subsystem1/1','Subsystem1/2'},'autorouting','smart')
Input Arguments
sys
— Model or subsystem to add line to
character vector
Model or subsystem to add the line to, specified as a character vector.
Example: 'vdp'
Example: 'f14/Controller'
out
— Block output port to connect line from
block name/port number or name | port handle | array of port designators
Block output port to connect line from, specified as one of these values:
The block name, a slash, and the port number. For a state port, use the port name
State
instead of a port number.The port handle that you want to connect from.
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.
in
— Block input port to connect line to
block name/port number or name | port handle | array of port designators
Block input port to connect line to, specified as one of these values:
The block name, a slash, and the port number. Use a port name instead of a port number for these ports:
Enable port — Use
Enable
. For example, use this port name for the enable port on enabled subsystems.Trigger port — Use
Trigger
. For example, use this port name for the trigger port on triggered subsystems.Action port — Use
Ifaction
. For example, use this port name for the action port on if-action and switch-case-action subsystems.
The port handle that you want to add the line to.
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.
autoOption
— Type of automatic line routing
'off'
(default) | 'on'
| 'smart'
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
— Points of the line to draw
matrix
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
h
— Line
handle
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)