getSimulinkBlockHandle Inverse - Get path from handle

7 views (last 30 days)
I've recently found the the BlockPath class can provide the full address of a block, even if that block is inside a referenced model. The path from get_param(h,'Parent') will simply stop at the referenced model, but BlockPath keeps going to the root model.
This is useful, but I can't find a useful way to make a BlockPath other than with gcbp.
Is there a way to turn a handle into a BlockPath? I'm working with 2019b due to other toolchain limitations
Thanks

Accepted Answer

Anshuman
Anshuman on 1 Aug 2023
Hi Mark, in MATLAB R2019b, there is no direct way to convert a handle into a BlockPath object. The BlockPath class was introduced in MATLAB R2020a. However, you can still achieve similar functionality by manually constructing the block path using the handle information.
Here's an example of how you can manually construct a block path given a handle in MATLAB R2019b:
% Assuming you have a handle 'h' to the block
blockPath = getfullname(h); % Get the full name of the block
% If the block is inside a referenced model, get the parent system
parentSystem = get_param(blockPath, 'Parent');
% Iterate until you reach the root model
while ~isempty(parentSystem) && ~strcmp(parentSystem, 'root')
blockPath = [get_param(parentSystem, 'Name'), '/', blockPath];
parentSystem = get_param(parentSystem, 'Parent');
end
% Display the block path
disp(blockPath);
In this example, the 'getfullname' function retrieves the full name of the block associated with the handle. Then, by iterating through the parent systems using 'get_param', you can construct the complete block path, including any referenced models, until you reach the root model.
Please note that this approach relies on the assumption that the block hierarchy is consistent with the block names and parent-child relationships. If there are multiple blocks with the same name in the model, this method might not produce the correct block path.
Hope it helps!
  1 Comment
Mark
Mark on 7 Aug 2023
Many thanks,
I had implemented a similar function myself in the meantime. I just throught that there must be some functionality since gcbp was working......but it looks like not for 2019b.

Sign in to comment.

More Answers (0)

Categories

Find more on Model References in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!