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

When you want to make multiple function calls that require you to specify a block as an input argument, consider using block handles. This example shows how to get a block handle.

Open the example. Then, load the f14 model.

load_system('f14')

Get the handle of the Signal Generator block named Pilot.

h=getSimulinkBlockHandle('f14/Pilot')
h = 106.0007

You can use the handle in subsequent function calls, for example, to the get_param or set_param functions.

Use the block handle to get the value of the Wave form parameter.

get_param(h,'WaveForm')
ans = 
'square'

Use the block handle to set the value of the Wave form parameter to Sine.

set_param(h,'WaveForm','Sine')

To see the change, get the value of the Wave form parameter again.

get_param(h,'WaveForm')
ans = 
'sine'

This example shows how to load a model and get the handle of a block in that model with a single function call.

Open the example. Then, load the f14 model and get the handle of the block named Pilot with a single call to the getSimulinkBlockHandle function.

h = getSimulinkBlockHandle('f14/Pilot',true)
h = 124.0009

You can use the handle in subsequent function calls, for example, to the get_param or set_param functions.

Use the block handle to get the value of the Wave form parameter.

get_param(h,'WaveForm')
ans = 
'square'

The getSimulationBlockHandle function returns the handle of the block specified in the function input arguments. Valid handles are greater than zero. If the function does not find the block, it returns -1.

This example shows how to check whether a model is loaded and contains a specific block using the getSimulationBlockHandle function.

Check whether the model f14 is loaded and contains a block named Pilot.

Check=getSimulinkBlockHandle('f14/Pilot') > 0
Check = logical
   0

If Check has a value of 0, either the f14 model is not loaded, or the f14 model does not contain the block named Pilot.

If Check has a value of 1, the model is loaded, and the model contains a block named Pilot.

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