Add line to Simulink model


h = add_line(sys,out,in)
h = add_line(sys,out,in,'autorouting',autoOption)
h = add_line(sys,points)



h = add_line(sys,out,in) adds a line in the model or subsystem 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 blocks are compatible for connecting.


h = add_line(sys,out,in,'autorouting',autoOption) connects blocks, specifying whether to route the lines around other blocks.


h = add_line(sys,points) adds a line drawn by (x,y) coordinate 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.


collapse all

Use the block port numbers to add a line to connect blocks.

Create a model and open it.


Add and position a Constant block and a Gain block.

add_block('simulink/Commonly Used Blocks/Constant','connect_model/Constant');
add_block('simulink/Commonly Used Blocks/Gain','connect_model/Gain');

Connect the blocks. Each block has one port, so specify port 1.


Get the port handles and connect the ports using add_line.

Open the model vdp.


Delete the line that connects the Mu gain block to the Sum block.


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 = 

  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.


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 routing to improve the look of the branched line.

Add a scope to the vdp model above the outport.

add_block('simulink/Commonly Used Blocks/Scope','vdp/Scope1');

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.


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.


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');
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');
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])

This example shows the effect of adding lines with and without autorouting options.

Create a model route. Display default block names.


Add blocks as shown. Add an inport and outport to each subsystem.

Add lines to connect the outputs from Subsystem to the inputs of Subsystem1.


Because you did not use the autorouting options, the function draws straight lines, which pass through the Gain block.

Delete the lines. Add lines again, this time using the autorouting option set to '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.


Input Arguments

collapse all

Model or subsystem to add the line to, specified as character vector.

Example: 'vdp' , 'f14/Controller'

Block output port to connect line from, specified as:

  • The block name, a slash, and the port name or number. Most block ports are numbered from top to bottom or from left to right. 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', 'Subsystem/2', h.Outport(1){'Subsystem/1','Subsystem/2'}

Block input port to connect line to, specified as:

  • The block name, a slash, and the port name or number. The port name on:

    • An enabled subsystem is Enable.

    • A triggered subsystem is Trigger.

    • If Action and Switch Case Action subsystems is Action.

  • 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', 'Subsystem/2', h.Inport(1), {'Subsystem/1','Subsystem/2'}

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

collapse all

Line created by add_line, returned as a handle.

Introduced before R2006a

Was this topic helpful?