Contenu principal

getSimulinkBlockHandle

R2026b

Get block handle from block path

Description

handle = getSimulinkBlockHandle(path) returns the numeric handle of the block at the location specified by path, provided it exists in a loaded model or library. The handle applies only to the current MATLAB® session. If the block is not found, the function returns -1. Library links are resolved where necessary.

Use the handle returned by getSimulinkBlockHandle to manipulate the block in subsequent function calls. Doing so is more efficient than using the block path. The number you see if you output the handle in the MATLAB Command Window is usually rounded. Do not use this value. Instead, assign the handle to a variable.

Use the getSimulinkBlockHandle function to check whether a block path is valid. This approach is more efficient than calling the get_param inside a try statement.

To get the block path from the block handle, use the getfullname function.

example

handle = getSimulinkBlockHandle(path,true) attempts to load the model or library containing the block at the location specified by path, and then checks if the block exists. No error is returned if the model or library is not found. Any models or libraries loaded this way remain in memory even if the function does not find a block with the specified path.

example

Examples

collapse all

Suppose you have a model named myModel on the MATLAB path. The model contains a Subsystem block named mySubsystem that contains a Gain block named myBlock. Get the handle of myBlock and use the handle to change the gain to 5.

Load the model.

load_system("myModel");

Get the handle.

myPath = "myModel/mySubsystem/myBlock";
h = getSimulinkBlockHandle(myPath);

Change the gain to 5.

set_param(h,Gain="5");

Suppose you have a model named myModel on the MATLAB path. The model contains a Gain block named myBlock.

Load the model and get the handle of myBlock with one function call.

myPath = "myModel/myBlock";
h = getSimulinkBlockHandle(myPath,true);

Suppose you have a model named myModel on the MATLAB path. The model contains a Subsystem block named mySubsystem that contains three blocks: myBlock1, myBlock2, and myBlock3.

Load the model and get the handle of all three blocks in the subsystem with one function call.

pathRoot = "myModel/mySubsystem/myBlock";
myPaths = [pathRoot+"1";pathRoot+"2";pathRoot+"3"];
h = getSimulinkBlockHandle(myPaths,true);

Suppose you have a model named myModel on the MATLAB path. The model contains a Subsystem block named mySubsystem. Check whether the subsystem contains the block named myBlock.

Load the model.

load_system("myModel");

Check whether the subsystem contains the block named myBlock.

myPath = "myModel/mySubsystem/myBlock";
hasBlock = getSimulinkBlockHandle(myPath)>0

If the value of hasBlock is 1, the subsystem contains the block named myBlock. If the value is 0, the subsystem does not contain the block.

Input Arguments

collapse all

Path of one or more blocks, specified as a string, string array, character vector or cell array of character vectors.

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

Example: "myModel/mySubsystem/myBlock"

Example: ["myModel/myBlock1";"myModel/myBlock2"]

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

Output Arguments

collapse all

Numeric handle of a block, returned as a numeric scalar. If you specify multiple block paths, the function returns a numeric array of handles. Valid handles are always greater than zero. If the function does not find the block, it returns -1.

Data Types: double | array of doubles

Version History

Introduced in R2015a