Main Content

add_line

R2026b

(Not recommended) Add line to Simulink model

add_line is not recommended. Use Simulink.connectBlocks instead (since R2024b). For information on updating your code, see Version History.

Description

handle = add_line(sys,out,in) adds signal lines in the specified model or subsystem that connect the specified output ports to the specified input ports. Connecting multiple input ports to the same output port is not supported. The ports must be unconnected and must be in the same location in the model hierarchy, either at the top level or in the same component. Each pair of ports that you want to connect must be compatible.

This syntax draws the most direct route from port to port, for example, diagonal lines or lines that go through other blocks.

example

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

example

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.

example

Examples

collapse all

Suppose you have a loaded model named myModel. The model contains a Gain block named myBlock1 and a Scope block named myBlock2 with two input ports. Connect the output port of the Gain block to the bottom input port of the Scope block.

Determine the port path of the output port. The port path is the block name followed by a slash and the port number. Since the Gain block only has one output port, the port number is 1.

out = "myBlock1/1";

Determine the port path of the input port. The Scope block has two ports. If you pause your pointer on the ports, you can see that the port number of the bottom port is 2.

in = "myBlock2/1";

Connect the ports.

sys = "myModel";
add_line(sys,out,in);

Suppose you have a loaded model named myModel. The model contains a Gain block named myBlock1 and a Scope block named myBlock2 with two input ports. Connect the output port of the Gain block to the bottom input port of the Scope block.

Get the handle of the model.

sys = get_param("myModel","Handle");

Get all port handles of the Gain block.

hGain = get_param("myModel/myBlock1","PortHandles");

Get the output port handle of the Gain block.

out = hGain.Outport;

Get all port handles of the Scope block.

hScope = get_param("myModel/myBlock2","PortHandles");

Get the bottom input port handle of the Scope block.

in = hScope.Inport(2);

Connect the ports.

add_line(sys,out,in);

Suppose the blocks in the previous example are positioned such that the Scope block is directly below the Gain block.

Connect the blocks as you did in the previous example.

The connection is a diagonal signal line that overlaps the corners of the block.

Delete the signal line.

delete_line(sys,out,in)

Connect the blocks using smart routing.

add_line(sys,out,in,autorouting="smart");

The connection is a signal line consisting of orthogonal line segments that do not overlap the blocks.

Suppose you have a loaded model named myModel that contains a Subsystem block named mySubsystem. The subsystem contains an If block named myBlock1 with two output ports, and a Scope block named myBlock2 with two input ports. Connect the output ports of the If block to the input ports of the Scope block.

Get the handle of the model.

sysPath = "myModel/mySubsystem";
sys = get_param(sysPath,"Handle");

Get all port handles of the If block.

hIf = get_param(sysPath+"/myBlock1","PortHandles");

Get the output port handles of the If block.

out = hIf.Outport;

Get all port handles of the Scope block.

hScope = get_param(sysPath+"/myBlock2","PortHandles");

Get the input port handles of the Scope block.

in = hScope.Inport;

Connect the blocks with one command.

add_line(sys,out,in);

Suppose you have a loaded model named myModel that contains a Sine Wave block named myBlock1, a Gain block named myBlock2, and a Sum block named myBlock3. The Sine Wave block is connected to the Gain block. Branch that signal line and connect the Sine Wave block to the Sum block.

Get the handle of the model.

sysName = "myModel";
sys = get_param(sysName,"Handle");

Get all port handles of the Sine Wave block.

hSine = get_param(sysName+"/myBlock1","PortHandles");

Get the output port handle of the Sine Wave block.

out = hSine.Outport;

Get all port handles of the Sum block.

hSum = get_param(sysName+"/myBlock3","PortHandles");

Get one of the input port handles of the Sum block.

in = hSum.Inport(1);

Connect the blocks. The signal line branches to create the specified connection.

add_line(sys,out,in);

Suppose you have a loaded model named myModel that contains a Constant block named myBlock1 and a Gain block named myBlock2. Connect the blocks using port coordinates.

Get the port locations of the output port on the Constant block.

portConnConst = get_param("myModel/myBlock1","PortConnectivity");
portPosConst = portConnConst.Position;

Get the port locations of the input port on the Gain block. The Gain block has two ports, so querying the position returns two positions. The first position corresponds to the input port.

portConnGain = get_param("myModel/myBlock2","PortConnectivity");
portPosGain = portConnGain(1).Position;

Combine the positions of the output and input ports you want to connect into a matrix containing the position of the output port in the first row and the position of the input port in the second row.

pos = [portPosConst;portPosGain];

Connect the ports.

add_line("myModel",pos);

Input Arguments

collapse all

Model or subsystem containing the blocks you want to connect. Specify the model as a handle or name, and the subsystem as a handle or the block path of the corresponding Subsystem block. Format a handle as a numeric scalar. Format a name or path as a string or character vector.

For information about getting handles, see Get Handles and Paths.

Example: "myModel"

Example: "myModel/mySubsystem"

Data Types: double | string | character vector

Block output ports to connect, specified as one of these values:

  • Port handle or array of port handles

    Use "PortHandles" with get_param to get the handles.

  • Port path or array of port paths

    The port path is the name of the block to which the port attaches followed by a slash and the port number or name, for example, "myBlock/1". For conditionally executed subsystem blocks and the Integrator block, use port names. For more information, see Get Handles and Paths.

Format port handles as numeric scalars or numeric arrays. Format port paths as strings, character vectors, or cell arrays of character vectors. If you specify an array, the out array must have the same dimensions as the in array.

For information about getting handles and paths, see Get Handles and Paths.

Example: h.Outport(1)

Example: "myBlock/1"

Example: {'myBlock1/1','myBlock2/1'}

Tips

Data Types: double | array of doubles | string | character vector | cell array of character vectors

Block input ports to connect, specified as one of these values:

  • Port handle or array of port handles

    Use "PortHandles" with get_param to get the handles.

  • Port path or array of port paths

    The port path is the name of the block to which the port attaches followed by a slash and the port number or name, for example, "myBlock/1". For conditionally executed subsystem blocks and the Integrator block, use port names. For more information, see Get Handles and Paths.

Format port handles as numeric scalars or numeric arrays. Format port paths as strings, character vectors, or cell arrays of character vectors. If you specify an array, the in array must have the same dimensions as the out array.

Example: h.Outport(1)

Example: "myBlock/1"

Example: {'myBlock1/1','myBlock2/1'}

Tips

For information about getting handles and paths, see Get Handles and Paths.

Data Types: double | array of doubles | string | character vector | cell array of character vectors

Type of automatic line routing around other blocks:

  • "off" — No automatic line routing

  • "on" — Automatic line routing

  • "smart" — Automatic line routing that takes advantage of the blank spaces on the canvas and avoids overlapping other lines and labels

Data Types: string | character vector

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 Simulink Editor before any canvas resizing.

Example: [100 300; 200 300; 200 300; 200 500]

Data Types: numeric array

Output Arguments

collapse all

Signal line the function creates, returned as a handle.

Version History

Introduced before R2006a

collapse all