Main Content

getSimulinkBlockHandle

Get block handle from block path

Description

example

handle = getSimulinkBlockHandle(path) returns the numeric handle of the block specified by path, if it exists in a loaded model or library. Returns -1 if the block is not found. Library links are resolved where necessary.

Use the numeric handle returned by getSimulinkBlockHandle to manipulate the block in subsequent calls to get_param or set_param. This approach is more efficient than making multiple calls to these functions using the full block path. Do not try to use the number of a handle alone (e.g., 5.007) because you usually need to specify many more digits than MATLAB® displays. Assign the handle to a variable and use that variable name to specify a block. The handle applies only to the current MATLAB session.

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

example

handle = getSimulinkBlockHandle(path,true) attempts to load the model or library containing the specified block 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.

Examples

collapse all

Get the handle of the Pilot block.

load_system('f14')
handle = getSimulinkBlockHandle('f14/Pilot')
handle =

  562.0004

You can use the handle in subsequent calls to get_param or set_param.

Load the model f14 if necessary (by specifying true), and get the handle of the Pilot block.

handle = getSimulinkBlockHandle('f14/Pilot',true)
handle =

  562.0004

You can use the handle in subsequent calls to get_param or set_param.

Check whether the model f14 is loaded and contains a block named Pilot. Valid handles are always greater than zero. If the function does not find the block, it returns -1.

valid_block_path = getSimulinkBlockHandle('f14/Pilot') > 0
valid_block_path =

     0

The model contains the block but the model is not loaded, so this command returns 0 because it cannot find the block.

Using getSimulinkBlockHandle to check whether a block path is valid is more efficient than calling get_param inside a try statement.

Input Arguments

collapse all

Block path name, specified as a character vector or a cell array of character vectors.

Example: 'f14/Pilot'

Data Types: char

Output Arguments

collapse all

Numeric handle of a block, returned as a double or an array of doubles. Valid handles are always greater than zero. If the function does not find the block, it returns -1. If the path input is a cell array of character vectors, then the output is a numeric array of handles.

Data Types: double

Version History

Introduced in R2015a