Search code examples
matlabpathsimulink

Simulink: Get Path of Blocks Higher on the Hierarchy


In Matlab, the get_param and set_param commands require the exact directory of a block.
The gcb command can give you the path of a currently selected block.
What is the best method to determine blocks paths relative to this block?

For example, relative path identifiers sometimes use periods.

To find the current path, use './'.
To find the parent path, use '../'.
To find the parent of the parent, use '../../'.

These do not function in conjunction with gcb however.

MWE

In Simulink, if I create a block and select it,
and then type into the Matlab window:

get_param([gcb], 'ObjectParameters')

I am provided with a list of block parameters,
any of which may be altered using set_param([gcb], <parameter>, <value>).

Included in the list of parameters is Parent, which provides the parent path.
Is there a command which can take a path and remove until the next '/'?
(Or more usefully, a command which acts as a reverse fullfile?)
(Someone mentioned regular expressions, so I'm looking into those.)

Also, (less important to me, but for posterity),
is there a command which can find children paths?


Solution

  • You can use get_param(<blockname>, 'Parent') for parent of the current block. To get parent of parent and higher layers you need to create your own function like this:

    function blk = getParent(blk, n)
    for k=1:n
        blk = get_param(blk, 'Parent');
    end
    end
    

    So getParent(gcb, 1) gives you the parent, getParent(gcb, 2) gives you the parent of the parent and so on.

    To find the children you should use find_system together with SearchDepth argument.

    find_system(gcb) % All children regardless of depth
    
    find_system(gcb, 'SearchDepth', 1) % Immediate children
    
    find_system(gcb, 'SearchDepth', 2) % Immediate children and their children
    

    Note that if some of the children are library links you need to use FollowLinks option as well.