Main Content

Get Handles and Paths

To take an action programmatically in Simulink®, you must specify the target of the action. For example, if you want to change the value of a Constant block, you must specify which block you want to edit. Typically, you take action programmatically by running a function, and you specify the target object in the function input arguments. For example, this command changes the value of the Constant block named myBlock in the model named myModel to 5.

set_param("myModel/myBlock",Value="5")

The target object can be a model, a component—such as a subsystem or library—or a model element, such as a block or signal line. To specify the target object, use a handle or path.

A path is a string (preferred) or character vector that specifies the location of a target object in the model hierarchy relative to the root level of the model. Generally, the path starts with the model name and ends with the name of the target object, for example, "myModel/mySubsystem/myBlock". The names in the path must match the names in the model hierarchy exactly, including white spaces. For information about how to specify paths that contain multiline names or special characters, see Specify Paths That Contain Multiline Names and Special Characters.

A handle is a MATLAB® data type that stores an association to a target object. When you output the value of a handle, the MATLAB Command Window displays a number. Do not try to use the displayed number, because the command window does not display all the digits and handles do not persist across Simulink sessions. Instead, store the handle value in a variable, and then use the variable to specify the target object.

To take action using a function, in the function documentation, check whether the function accepts only handles, only paths, or both as input arguments. If the function accepts both handles and paths, consider using handles. Simulink processes handles faster than paths, and the handle of a target object persists even when the path of the target object changes. For most target objects, if you have a path, you can get the corresponding handle and vice versa. For more information, see Convert Between Handles and Paths.

You can get the handles and paths of target objects whose names and locations you know, or you can run a programmatic search to get the handles and paths of target objects that fit certain criteria. For information about how to search, see Search Programmatically.

Specify Model, Library, or Subsystem

To programmatically specify models, libraries, or subsystems, use a handle or filename. If the model, library, or subsystem is referenced by another model, library, or subsystem, you might need to specify the reference block instead. For example, to take action on a subsystem that is referenced by a model, you might need to specify the subsystem, or you might need to specify the Subsystem Reference block. First, determine which target object to specify. Then, determine how to specify the target object.

Determine Which Target Object to Specify

To take programmatic action using a function, check the function documentation to determine what type of target object you can specify in the input arguments.

If the function can act on more than one type of target object, use the interactive approach to the action to determine what to specify. When you edit a model, library, or subsystem interactively, you can take certain actions by opening the component and interacting with the contents. To make the same edits programmatically, specify the component. You can take other actions by interacting with blocks that reference or contain the component, for example Model or Subsystem blocks. To make the same edits programmatically, specify the block.

For example, suppose you have a subsystem that is saved to a subsystem file named mySubsystem. You also have a model named myModel. The model contains a Subsystem Reference block named mySubsystemRefBlock that references the subsystem.

To change the canvas color of the subsystem, use the set_param function. In the function input arguments, specify the subsystem.

set_param("mySubsystem",ScreenColor="green")

To see the change in the model, you must update the model diagram. To update the diagram, use the set_param function. In the function input arguments, specify the model.

set_param("myModel",SimulationCommand="Update")

To treat the subsystem as atomic, use the set_param function. In the function input arguments, specify the Subsystem Reference block.

set_param("myModel/mySubsystemRefBlock",TreatAsAtomicUnit="on")

To get a list of the parameters that you can get and set programmatically when you specify a model, first, load the model. Enter this command in the MATLAB Command Window, where myModel is the model filename.

load_system("myModel")

Then, enter this command.

get_param("myModel","ObjectParameters")

To get a list of the parameters that you can get and set programmatically when you specify a block, load the model containing the block. Then, enter these commands, where path is the block path.

param = get_param(path,"ObjectParameters");
param = fieldnames(param)

The list of parameters output by these commands does not include mask parameters. To get a list of the mask parameters, enter these additional commands.

paramMaskStruct = get_param(path,"DialogParameters");
paramMask = fieldnames(paramMaskStruct)

If you determine that you need to specify a model, library, or subsystem, see the next section. If you need to specify a reference block, see Specify Block.

Specify Model, Library, or Subsystem

To programmatically specify a model, a library, or a subsystem saved to a file, use a handle or filename. For example, to set the solver of a model named myModel with the handle h to ode45, use one of the commands in the Example column of the table.

OptionExample
Handle
set_param(h,SolverName="ode45")
Filename
set_param("myModel",SolverName="ode45")

If a file is not in the current folder or on the MATLAB search path, then using the filename to specify the model, library, or subsystem the file contains is not supported. In this case, take one of these actions.

  • Specify the model, library, or subsystem using the file path ending with the filename, for example, C:\Users\user1\Documents\myFolder\myModel.

  • Change directories to the folder containing the file using the cd function.

  • Add the file to the MATLAB search path using the addpath function.

  • Move the file into the current folder.

Get Model, Subsystem, or Library Handle

You can programmatically get the handle of a model, a library, or a subsystem saved to a file from the filename or while taking certain actions, you can get the handle and filename of the current system, and you can get the handle of the harness model for a protected model.

To get the handle of a model, a library, or a subsystem saved to a file from the filename, load the model, library, or subsystem. Then, use this command, where myModel is the filename.

h = get_param("myModel","Handle")

The table shows how you can get the handle of a model, a library, or a subsystem saved to a file while taking an action. Use the function documentation to select a syntax that outputs the model, library, or subsystem handle.

ActionFunctionExample
Create a model, library, or subsystem based on the default template.new_system
h = new_system("myModel")
Create a model or project based on the template you specify.Simulink.createFromTemplate
h = Simulink.createFromTemplate("myTemplate")
Load a model, library, or subsystem.load_system
h = load_system("myModel")
Find a model, subsystem, or library.find_system
h = find_system("myModel",FindAll="on")
Find loaded models and libraries.Simulink.allBlockDiagrams
h = Simulink.allBlockDiagrams("model")

To get the handles of multiple models, libraries, or subsystems saved to files at once using the load_system, get_param, new_system, or find_system function, specify the model filenames as one of the options in the table.

How to Specify Multiple Models, Libraries, or SubsystemsExample
Filenames specified as string array (preferred)
models = ["myModel1";"myModel2";"myModel3"];
h = get_param(models,"Handle")
Filenames specified as cell array of character vectors (strings are recommended over character vectors)
models = {'myModel1';'myModel2';'myModel3'};
h = get_param(models,"Handle")

The table shows how to get the handle and path of the current system and the filename of the top-level model of the current system. For the definition of the current system, see gcs.

ActionFunctionExample
Get filename of current system.gcs
name = gcs
Get handle of current system.

gcs, get_param

h = get_param(gcs,"Handle")
Get the filename of the top-level model of the current system.bdroot
name = bdroot

To get the handle of the harness model for a protected model, use the Simulink.ModelReference.protect (Embedded Coder) function.

h = Simulink.ModelReference.protect("myModel",Harness=true)

Specify Block

To programmatically specify a block, use the block handle or path. For example, suppose you have a model named myModel with the handle h. The model contains a Gain block named myBlock. To get the value of the gain, use one of the commands in the Example column of the table.

OptionExample
Handle
gain = get_param(h,"Gain")
Path
gain = get_param("myModel/myBlock","Gain")

To take inventory of blocks in referenced models, use a Simulink.BlockPath object instead of a block path. A Simulink.BlockPath object uniquely identifies a block within a model hierarchy, even when the model hierarchy references the same model multiple times. If you do not have a Simulink license, use a Simulink.SimulationData.BlockPath object instead.

Get Block Path and Block Path Object

The block path specifies the location of the block in the model hierarchy relative to the root level of the model. The block path starts with the model name and ends with the block name, for example, myModel/mySubsystem/myBlock. Instead of typing a block path manually, you can get the path from the context menu of the block. Open the model containing the block, right-click the block, select Copy Path, and then paste the path into your command. Remember to add quotes around the pasted text to convert the text into a string or a character vector.

The Matlab Command Window displays part of a command. The command calls the set_param function. The input arguments are empty. A Simulink window appears. The pointer right-clicks a block and selects Copy Path. The path is pasted at the end of the command in the Matlab Command Window. This command is completed and executed. In the Simulink window, the block has a red outline.

To get the block path of a Library Browser block, see Get Handle or Path of Library Browser Block. To get the block path of the current block, see Get Handle, Path, or Block Path Object of Current Block.

To get a block path object, use the Simulink.BlockPath, Simulink.SimulationData.BlockPath, or gcbp function. For example, suppose you have a model named myModel1 that contains a Model block named myBlock1. The block references a model named myModel2. The referenced model contains a Gain block named myBlock2. Use this command to get the block path object of the Gain block.

myObj = Simulink.BlockPath(...
        {'myModel1/myBlock1',...
        'myModel2/myBlock2'})

Get Block Handle

You can get a block handle from a block path, the handle of a port on the block, the handle of a line connected to the block, or by taking certain actions. To get the handle of a Library Browser block, see Get Handle or Path of Library Browser Block. To get the handle of the current block, see Get Handle, Path, or Block Path Object of Current Block.

To get a block handle from a block path and load the model in one command, use the getSimulinkBlockHandle function with this syntax. path is the block path, specified as a string (preferred) or character vector.

h = getSimulinkBlockHandle(path,true)

To get a block handle from a block path in a loaded model, use this syntax.

h = getSimulinkBlockHandle(path)

To get the handles of multiple blocks at once using the getSimulinkBlockHandle function, specify the block paths as one of the options in the table.

How to Get Handles of Multiple Blocks from Block PathsExample
Block paths specified as string array (preferred)
paths = ["myModel/myBlock1";"myModel/myBlock2";"myModel/myBlock3"];
h = getSimulinkBlockHandle(paths)
Block paths specified as cell array of character vectors (strings are recommended over character vectors)
paths = {'myModel/myBlock1';'myModel/myBlock2';'myModel/myBlock3'};
h = getSimulinkBlockHandle(paths)

To get the handle and path of the block that a specific port is on, use these commands, where hPort is the port handle.

path = get_param(hPort,"Parent")
h = getSimulinkBlockHandle(path)

To get the handles and paths of the blocks for multiple ports at once, specify hPort as a vector of port handles.

To get the handles of the blocks a signal line connects, use these commands, where hLine is the line handle. The SrcBlockHandle parameter specifies the handle of the block to whose output port the signal line connects, and the DstBlockHandle parameter specifies the handle of the block to whose input port the signal line connects.

hSrc = get_param(hLine,"SrcBlockHandle")
hDst = get_param(hLine,"DstBlockHandle")

Branched signal lines have multiple handles: one for the whole signal line including all branches, and one for each branch. If hLine is the handle of a whole signal line, the second command outputs the blocks connected to all branches of the signal line. If hLine is the handle of a branch, the second command outputs the block connected to that branch. To run these commands on multiple signal lines at once, specify hLine as a vector of line handles.

The table shows how you can get the handle of a block while taking an action. Use the function documentation to select a syntax that outputs the block handle.

ActionFunctionExample
Add blocks.add_block
h = add_block("simulink/Sinks/Scope","myModel/myBlock")
Find blocks.find_system
h = find_system("myModel",FindAll="on",Type="block")
Simulink.findBlocks
hModel = get_param("myModel","Handle");
h = Simulink.findBlocks(hModel)
Simulink.findBlocksOfType
h = Simulink.findBlocksOfType("myModel","Integrator")
Replace blocks.replace_block
path = replace_block("myModel","Scope","simulink/Sinks/To Workspace")
h = getSimulinkBlockHandle(path)

Connect blocks.

Connecting blocks in different levels of the model hierarchy can create new blocks, such as Inport blocks in a subsystem.

Simulink.connectBlocks

See Get Types, Handles, and Paths of Model Elements Created by Simulink.connectBlocks Function for details.

connection = simulink.connectBlocks(hSrcPort,hDstPort);
transits = connection.getTransits;
isBlock = arrayfun(@(x)x.getType=="block",transits);
blockTransits = transits(isBlock);
h = arrayfun(@(x)x.getHandle,blockTransits)

Get Handle or Path of Library Browser Block

To get the handle or path of a Library Browser block:

  1. Open the quick insert menu by double-clicking the model canvas.

  2. Enter the block name.

  3. Use your arrow keys to select the block in the search results.

  4. On the details pane, click the name of the library that contains the block. The library opens.

  5. Select the block.

  6. Get the handle of the current block using this command.

    gcbh

    Get the path of the current block using this command.

    gcb

Quick insert menu with the word "derivative" in the search box, the Derivative block selected in the search results, and the pointer paused on the name of the Simulink library on the details pane

Get Handle, Path, or Block Path Object of Current Block

The table shows how to get the handle, path, or SimulinkBlockPath object of the current block. For the definition of the current block, see gcb.

ActionFunctionExample
Get handle.gcbh
h = gcbh
Get path.gcb
path = gcb
Get Simulink.BlockPath object.gcbp
pathObject = gcbp

Specify Port

To programmatically specify ports, use the port handle or path. For example, suppose you have a model named myModel. The model contains an unconnected block named myBlock1 with a single output port, and an unconnected block named myBlock2 with a single input port. To connect the two blocks, use one of the commands in the Example column of the table. hSrcPort is the output port handle. hDstPort is the input port handle.

OptionExample
Handle
Simulink.connectBlocks(hSrcPort,hDstPort)
Path
Simulink.connectBlocks("myModel/myBlock1/1","myModel/myBlock2/1")

Get Port Path

The port path specifies the location of the port in the model hierarchy relative to the root level of the model. Generally, the path starts with the model name and ends with the port number, for example, myModel/myBlock/1.

For example, suppose you have a model named myModel containing an Inport block and a Scope block with three ports. This command connects the port on the Inport block to the bottom port on the Scope block using the port paths myModel/Inport/1 and myModel/Scope/3, respectively.

Simulink.connectBlocks("myModel/Inport/1","myModel/Scope/3")

Instead of typing a port path manually, you can get the path from the context menu of the port. Open the model containing the port, right-click the port, select Copy Path, and then paste the path into your command. Remember to add quotes around the pasted text to convert the text into a string or a character vector.

The Matlab Command Window displays part of the command to programmatically connect the Inport block to the Scope block. The path of the bottom port of the Scope block is missing. A Simulink window appears. In the Simulink Window, the pointer right-clicks the bottom port of the Scope block and selects Copy Path. The path is pasted at the end of the command in the Matlab Command Window. The command is completed and executed. In the Simulink window, the Inport and Scope block are connected with a signal line.

The described port path format has some exceptions. One exception pertains to conditionally executed subsystems. The path that specifies the control input port on a conditionally executed subsystem ends with the port name instead of a port number, for example, myModel/Triggered Subsystem/Trigger. For more port names, see Get Port Name.

Another exception pertains to buses. Suppose you want to query or edit the attribute value of a bus or bus element at a bus element port. For example, suppose you want to get or set the minimum value of a bus element at the point where the bus enters a subsystem. Use the get_param or set_param function. In the input arguments, specify the bus or bus element using a path. Getting bus or bus element paths using the Copy Path context menu option is not supported. Enter the path in your commands manually.

If the In Bus Element or Out Bus Element block corresponding to the bus element port is at the root level of a model, start the path with the model filename. If the block is at the root level of a subsystem, start with the block path. To query or edit the attribute value of a bus, append a forward slash and the port name to the end of the path. To query or edit the attribute value of a bus element, append a forward slash and the bus element path. For a bus element port, the bus element path provides the hierarchy from the top-level bus to the target element, separating each name in the hierarchy with a dot.

For example, suppose you have a model named myModel. The model contains a bus that connects to a Subsystem block named mySubsystem. Inside the subsystem is an In Bus Element block named myBlock that selects the bus element named Step from the bus at the port named InBus. This path specifies the bus element named Step at the Subsystem block port.

path = "myModel/mySubsystem/InBus.Step";

The subsystem named mySubsystem is shown with the content preview turned on. Inside the subsystem, an In Bus Element block named myBlock with the label InBus.Step connects to an Out Bus Element block named myBlock2 with the label OutBus.Step.

To get the port name of an In Bus Element or Out Bus Element block, use this command. block is the block handle or path.

portName = string(get_param(block,"PortName"));

To get the name of the bus element that an In Bus Element or Out Bus Element block selects, use this command.

elemName = string(get_param(block,"Element"));

To get the port name or selected bus element of multiple In Bus Element or Out Bus Element blocks at once, specify block as a vector of handles or as a set of paths in a string array or cell array of character vectors (strings are recommended over character vectors).

Get Port Name

The port name of the input port of a Simulink block is Inport. The port name of the output port is Outport.

The table lists the names of the control input ports for different types of conditionally executed subsystems.

Triggered Subsystem block with a callout labeling the control input port

Conditionally Executed SubsystemPort Name
TriggeredTrigger
EnabledEnable
Enabled and Triggered

Trigger

Enable

ResettableReset
If ActionIfaction
Switch Case ActionIfaction
Function-CallTrigger
Message PollingTrigger
Message TriggeredTrigger

The port name of the state port on the Integrator block is State.

Integrator block with the State port visible and highlighted

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 expressed as a string or character vector, for example, "myModel/myBlock".

set_param(path,ShowStatePort="on")

Get Port Handle

You can get the handles of the ports a specific signal line connects, or you can get the handles of the ports on a specific block. To get the handles of the ports a signal line connects, use these commands, where hLine is the line handle. The SrcPortHandle parameter specifies the handle of the output port to which the signal line connects, and the DstPortHandle parameter specifies the handle of the input port to which the signal line connects.

hSrc = get_param(hLine,"SrcPortHandle")
hDst = get_param(hLine,"DstPortHandle")

To run these commands on multiple signal lines at once, specify the signal lines as a vector of handles.

To get all port handles of a block, use this command, where block is the block handle or path.

h = get_param(block,"PortHandles")

To get the handles of the input and output ports of the block, enter these additional commands.

hIn = h.Inport
hOut = h.Outport

If the block represents a conditionally executed subsystem, to get the handles of the control input port, enter an additional command of this form.

hCtrlPort = h.Trigger

The command uses dot notation and the port name to extract the handle of the control input port from h. In this case, the port name is Trigger. For more port names, see Get Port Name.

To get all port handles of multiple blocks at once, specify the blocks as one of the options in the table.

How to Specify Multiple BlocksExample
Vector of handles
blocks = [h1;h2;h3];
h = get_param(blocks,"PortHandles")
Set of block paths specified as a string array
blocks = ["myModel/myBlock1";"myModel/myBlock2";"myModel/myBlock3"];
h = get_param(blocks,"PortHandles")
Set of block paths specified as a cell array of character vectors (strings are recommended over character vectors)
blocks = {'myModel/myBlock1';'myModel/myBlock2';'myModel/myBlock3'};
h = get_param(blocks,"PortHandles")

The output is a cell array. Each cell contains a structure that stores the port handles of one block. To get all the port handles of a specific block from h, use this command, where i is the cell index corresponding to the block.

hBlockPorts = h{i}

To get the handles in h of all ports of a specific type, you can use the arrayfun function. For example, to get all output port handles stored in h, use these commands.

hFunc = @(t) h{t}.Outport;
i = 1:length(h);
hOutports = arrayfun(hFunc,i)

The first command specifies a function that takes the value of t as input. The function gets the handles of all output ports stored in the cell of h with the index t. The second command populates a vector named i with all cell indices of h. The last command runs the function named hFunc for each index in i.

Specify Signal and Signal Line

To programmatically specify instance-specific properties of a signal or a discrete state, use the Simulink.Signal object. For example, use the Simulink.Signal object to specify the units or the maximum or minimum value of the signal.

To programmatically specify signal lines, use line handles, or use the handles or paths of the ports that the signal lines connect. For example, to delete a signal line that connects the single output port of a block named myBlock1 to the single input port of a block named myBlock2 in a model named myModel, use one of the commands in the Example column of the table. hLine is the handle of the signal line, hSrcPort is the handle of the output port of myBlock1, and hDstPort is the handle of the input port of myBlock2.

OptionExample
Line handles
delete(hLine)
Port handles
delete(hModel,hSrcPort,hDstPort)
Port paths
delete(hModel,"myModel/myBlock1/1","myModel/myBlock2/1")

A signal line without branches has one line handle. A branched signal line has multiple line handles: one for the signal line, and one for each branch.

A Sine Wave block named myBlock1 is connected to a Gain block named myBlock2 and an Integrator block named myBlock3. The connecting signal line has two branches, one connected to the block named myBlock2 and one connected to the block named myBlock3. The blocks are shown three times, once with the signal line including all branches highlighted, once with the branch connected to the block named myBlock2 highlighted, and once with the branch connected to the block named myBlock3 highlighted.

You can get the handle of the currently selected signal line or branch, the handles of the signal lines and branches connected to a specific block or port, and the handles of new signal lines you add to a model. You can also search for signal lines that meet certain criteria, for example, signal lines with label text you specify.

To get the line handle of the currently selected signal line or branch, use this command.

h = find_system(gcs,SearchDepth=1,FindAll="on",...
Type="line",Selected="on")

To get the handles of all signal lines connected to a block, use this command, where block is the block handle or path.

h = get_param(block,"LineHandles")

If a branched signal line connects to the output port of the specified block, the command returns the handle of the signal line. If a branched signal line connects to the input port of the specified block, the command returns the handle of the branch.

To get the handles of all signal lines connected to the output ports of the block, enter this additional command.

hOut = h.Outport

To get the handles of all signal lines connected to the input ports of the block, enter this additional command. For any branched signal lines, the command returns the handle of the branch connected to the block.

hIn = h.Inport

If the block represents a conditionally executed subsystem, to get the handle of the signal line connected to the control input port, enter an additional command in this form.

hCtrlPort = h.Trigger

The command uses dot notation and the port name to extract the handle of the signal line connected to the control input port from h. In this case, the port name is Trigger. For more port names, see Get Port Name. For any branched signal lines, the command returns the line handle of the branch connected to the block.

To get the line handles for multiple blocks at once, specify the blocks as one of the options in the table.

How to Specify Multiple BlocksExample
Vector of handles
blocks = [h1;h2;h3];
h = get_param(blocks,"LineHandles")
Set of block paths specified as a string array
blocks = ["myModel/myBlock1";"myModel/myBlock2";"myModel/myBlock3"];
h = get_param(blocks,"LineHandles")
Set of block paths specified as a cell array of character vectors (strings are recommended over character vectors)
blocks = {'myModel/myBlock1';'myModel/myBlock2';'myModel/myBlock3'};
h = get_param(blocks,"LineHandles")

The output is a cell array. Each cell contains a structure storing the handles of the signal lines connected to one block. To get all the line handles of a specific block from h, use this command, where i is the cell index corresponding to the block.

hBlockPorts = h{i}

To get the handles in h of all signal lines connected to ports of a specific type, you can use the arrayfun function. For example, to get all handles of signal lines connected to output ports stored in h, use these commands.

hFunc = @(t) h{t}.Outport;
i = 1:length(h);
hLines = arrayfun(hFunc,i)

The first command specifies a function that takes the value of t as input. The function gets the handles of all signal lines connected to output ports stored in the cell of h with the index t. The second command populates a vector named i with all cell indices of h. The last command runs the function named hFunc for each index in i.

To get the handles of the signal line or branch connected to a port, use this command, where hPort is the port handle.

h = get_param(hPort,"Line")

To get the line handles of multiple ports at once, specify the ports as a vector of handles.

To get the handle of a specific signal line or branch using a port handle, block handle, or block path, first determine which port or block you must specify to get the line handle. To get the handle of a signal line, specify the output port to which the signal line connects or the block on which that port is located. To get the handle of a branch, specify the input port to which the branch connects or the block on which that port is located.

The table shows examples for a model named myModel. The model contains a Sine Wave block named myBlock1 that connects to a Gain block named myBlock2 and a Integrator block named myBlock3.

ActionCommand

Get the handle of the whole signal line, including all branches.

The signal line connects to the output port of the block named myBlock1. Get the handles of all signal lines and branches connected to the block.

allLinesBlock1 = get_param("myModel/Block1","LineHandles");

Use dot notation and the port name to get the handle of the signal line connected to the output port of the block.

h = allLinesBlock1.Outport

To check your work, open the model. Then, use this command to select the line with the handle h.

set_param(h,"Selected","on")

Get the handle of the branch connected to the block named myBlock2.

The signal line connects to the input port of the block named myBlock2. Get the handles of all signal lines and branches connected to the block.

allLinesBlock3 = get_param("myModel/Block3","LineHandles");

Use dot notation and the port name to get the handle of the signal line connected to the output port of the block.

h = allLinesBlock3.Inport

To check your work, open the model. Then, use this command to select the line with the handle h.

set_param(h,"Selected","on")

Get the handle of the branch connected to the block named myBlock3.

Use the same approach as for the other branch.

allLinesBlock2 = get_param("myModel/myBlock2","LineHandles");
h = allLinesBlock2.Inport

To get the handle of signal lines you add to the model when you connect blocks using the Simulink.connectBlocks function, save the output to a variable.

connection = simulink.connectBlocks(hSrcPort,hDstPort);

Then, use these commands to extract the line handles from the output, replacing connection with the name of the variable to which you saved the output. For more information about how these commands work, see Get Types, Handles, and Paths of Model Elements Created by Simulink.connectBlocks Function.

transits = connection.getTransits;
isLine = arrayfun(@(x)x.getType=="line",transits);
lineTransits = transits(isLine);
h = arrayfun(@(x)x.getHandle,lineTransits)

To find signal lines that meet certain criteria, use the find_system function. By default, the find_system function only searches for models and blocks. To include annotations in the search, set the value of the FindAll search criteria option to "on". For example, to find all signal lines in a model with the handle hModel, use this command.

h = find_system(hModel,FindAll="on",Type="line")

To find all signal lines in the model with the label text myLabel, use this command.

h = find_system(hModel,Findall="On",Type="Line",Name="myLabel")

To find all signal lines containing the word myKeyword in the label text, use this command.

find_system(hModel,Findall="On",Regexp="on",...
CaseSensitive="off",Type="Line",Name=".*myKeyword.*")

Specify Annotation

To programmatically specify an annotation, use a handle or the name of the Simulink.Annotation object.

To get and set the values of annotation parameters, you can use the get_param and set_param functions and specify the annotation as a handle. Alternatively, you can specify the annotation using name of the Simulink.Annotation object and use dot notation. For example, to set the text of the Simulink.Annotation object named a with the handle h to New text, use one of the commands in the Example column of the table.

OptionExample
Handle
set_param(h,Text="New text")
Simulink.Annotation object
a.Text = "New text"

You can programmatically get the Simulink.Annotation object of a new annotation when you create the annotation, and you can get the Simulink.Annotation object and handle of existing annotations. You can also get the currently selected annotation, and the annotation from which a callback function was invoked.

To get the Simulink.Annotation object when you programmatically create an annotation, use the Simulink.Annotation function. For example, to create an annotation with the text My annotation text in a model named myModel and get the Simulink.Annotation object, use this command.

obj = Simulink.Annotation("myModel","My annotation text")

To get the handle of an annotation from the Simulink.Annotation object, use this command, where a is the Simulink.Annotation object.

h = a.Handle

To get the Simulink.Annotation object from an annotation handle, use the get_param function.

obj = get_param(h,"Object")

To get the handle of an existing annotation when you do not have the Simulink.Annotation object, programmatically search for the annotation using the find_system function. By default, the find_system function only searches for models and blocks. To include annotations in the search, set the value of the FindAll search criteria option to "on". For example, to find all annotations containing the text clutch in a model named myModel, use these commands.

find_system("myModel",Findall="on",Regexp="on",...
Type="Annotation",Name="clutch")

To get the Simulink.Annotation object of the currently selected annotation, use the getCurrentAnnotation function. To get the Simulink.Annotation object of the annotation from which a callback function was invoked, use the getCallbackAnnotation function.

To create a new area annotation, specify the built-in area annotation as a string (preferred) or character vector containing the text built-in/Area.

Convert Between Handles and Paths

When taking action programmatically, you might need to convert between handles and paths. For example, if you have a block path, but the input argument of the function you want to run must be a handle, you must get the block handle corresponding to the block path you have. The table shows how to make these conversions.

Current FormatFormat After ConversionSyntax
Model handleModel filename
name = get_param(h,"Name")

To get the handles of multiple models at once, specify the models as an array of handles.

Model filenameModel handle

If the model is not loaded, use this command.

h = load_system("myModel")

If the model is loaded, use this command.

h = get_param("myModel","Handle")

To get the handles of multiple models at once, specify the model filenames as a string array (preferred) or a cell array of character vectors.

Block handleBlock path
path = getfullname(h)

To get the paths of multiple blocks at once, specify the blocks as a vector of handles.

Block pathBlock handle
h = getSimulinkBlockHandle(path)

To get the handles of multiple blocks at once, specify the block path as a cell array of character vectors.

Port handlePort path that ends with port number
path = get_param(hPort,"Parent")+"/"+get_param(hPort,"PortNumber")

To get the paths of multiple ports at once, specify the ports as a vector of handles.

Port path that ends with port numberPort handle
splitPath = string(split(path,"/"));
portNum = str2num(splitPath(end));
blockPath = erase(path,"/"+portNum);
h = find_system(blockPath,Findall="on",Type="Port",PortNumber=portNum)

If multiple ports in your model have the same port path, h is a vector of handle values. To get the port type of the handle stored in h at index i, use this command.

get_param(h(i),"PortType")
Simulink.Annotation objectAnnotation handle
h = a.Handle
Annotation handleSimulink.Annotation object
obj = get_param(h,"Object")

Get Handle of Attached Block, Attached Ports, and Connected Signal Lines

If you have a block handle, using the get_param function, you can get the handles of the block ports and the handles of the signal lines connected to the block, and vice versa. The table shows how to use the handle of one of these target objects to get the handle of another. In the syntaxes listed in the table, block is the block handle or path, hPort is the port handle, and hLine is the line handle.

To run the commands on multiple ports or signal lines at once, specify these target objects as a vector of handles. To run the commands on multiple blocks at once, specify the blocks as a vector of handles or as a set of paths in a string array or cell array of character vectors. (Strings are recommended over character vectors.)

Handle You HaveHandle You Want to GetSyntax
BlockPorts of specified block
h = get_param(block,"PortHandles")
Signal lines connected to specified block
h = get_param(block,"LineHandles")
PortBlock to which specified port is attached
path = get_param(hPort,"Parent");
h = getSimulinkBlockHandle(path)
Signal lines connected to specified port
h = get_param(hPort,"Line")
Signal lineBlock to whose output port specified signal line connects
h = get_param(hLine,"SrcBlockHandle")
Blocks to whose input ports specified signal line connects
h = get_param(hLine,"DstBlockHandle")
Output port to which specified signal line connects
h = get_param(hLine,"SrcPortHandle")
Input ports to which specified signal line connects
h = get_param(hLine,"DstPortHandle")

See Also

| | | |

Topics