Removing path name from Simulink block name

2 views (last 30 days)
Leonid Guerchkovitch
Leonid Guerchkovitch on 24 Nov 2021
Commented: Paul on 24 Nov 2021
I'm working with a block replacement command for a large subsystem and I'm running into issues with the block naming.
I've created a sample subsystem Simulink model to illustrate.
When I run the command below, I create a list of all gain blocks in my model.
gain_blocks = getfullname(Simulink.findBlocksOfType('trial_model','Gain'));
To replace the blocks with another Simulink primitive library that's approved by the company standards, I use the command below and loop through all the instances of the Gain blocks
replace_block('trial_model','Name',gain_blocks(i),'do178Lib/Simulink/Math Operations/Gain');
The third arguement of the replace_block does not seem to support the full path of the Simulink blocks but rather their shortened names. Is there a quick way of stripping the path of each block from the string? Ideally, this would have to work and be adaptable for multi-level subsystems in Simulink.

Accepted Answer

Paul on 24 Nov 2021
I'm seeing the same thing, though I'm not sure that is correct base on the documentation. And it is certainly a limiting behavior. You can strip away the path to each block using string manipulation, but doing so can result in elements of gain_blocks that will have the same value
gain_blocks = {'untitled/Subsystem/Gain'; 'untitled/Subsystem/Gain1'; 'untitled/Subsystem/Gain2';
'untitled/Subsystem1/Gain'; 'untitled/Subsystem1/Gain1'; 'untitled/Subsystem1/Gain2'}
gain_blocks = 6×1 cell array
{'untitled/Subsystem/Gain' } {'untitled/Subsystem/Gain1' } {'untitled/Subsystem/Gain2' } {'untitled/Subsystem1/Gain' } {'untitled/Subsystem1/Gain1'} {'untitled/Subsystem1/Gain2'}
gain_blocks = extractAfter(gain_blocks,asManyOfPattern(wildcardPattern+"/"))
gain_blocks = 6×1 cell array
{'Gain' } {'Gain1'} {'Gain2'} {'Gain' } {'Gain1'} {'Gain2'}
When you then loop through gain_blocks, I guess you can manually pick which (or both) blocks named Gain (or Gain1, etc.) to replace. I assume that all will be replaced if using the 'noprompt' option.
But do you need to identify each block to replace by Name? If they are all of BlockType Gain, then would this work?
replace_block('trial_model','Gain','do178Lib/Simulink/Math Operations/Gain');
Unless after the replacement you need to copy some parameters from the old block to the new, then I guess you have to do them one at a time. In which case it seems like it will be hard to automate the process.
Paul on 24 Nov 2021
If I understand that command correctly, the sys input is the fully qualified block name? Hmm. My version (2020b) of the documentation says that sys is either the name of a model or a name of a subsystem. Does not say that it can be a name of a block (like it says eplicitly for "new"). But if it works, it works.
Seems like they should just have a command like
RepNames = replace_block(gain_blocks{i},'do178Lib/Simulink/Math Operations/Gain','noprompt');
i.e., replace the current block with the new block. Could even be vectorized to accept an array of current blocks.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!