Search code examples
matlabblocksimulink

Simulink escape with delete_block


I'm trying to used MATLAB delete_block function, which given a simulink block path deletes the block. Unfortunately if the name of the block includes a / it is unable to delete the block because of / escaping. If for example the full path is:

system/subsystem/outputBlock[rad/s]

delete_block fails deleting the block (without reporting any failure). In a warning message, which is not generated by the the delete_block function, I spotted that the path of the block is reported to be: system/subsystem/outputBlock[rad//s] (with the last / escaped). So probably what happens is that the path is escaped and is not found since instead of searching for system/subsystem/outputBlock[rad/s], delete_block searches for system/subsystem/outputBlock[rad//s]. To verify this, I tried by changing the name of the block manually by removing the last / and the delete_block function works. How can I delete blocks whose name in the pathname includes a /?


Solution

  • Hope, I can help here. The // is the escape sequence for the / character. If you want to delete blocks with the // in the name, I think it best to recurse down the tree to get the fully qualified name and escape any / at each point.

    % get the name of the block you want to delete, we'll just use gcb() for now
    blk = gcb;
    nameList = {};
    % get the name of this block
    currBlk = get_param(blk,'Name')
    nameList{end+1} = currBlk;
    % get the name of the root block diagram
    rootName = bdroot(blk)
    while( ~strcmp(get_param(blk,'Parent'),rootName) )
      currBlk = get_param(blk,'Parent');
      nameList{end+1} = get_param(currBlk,'Name');
    end
    nameList{end+1} = rootName;
    % for completeness, here's a naive attempt to reconstruct the path
    str='';
    for ii=length(nameList):-1:1
      str = [str strrep(nameList{ii},'/','//') '/' ];
    end
    str(end) = []; % get rid of the last '/'
    

    HTH!