Removing path name from Simulink block name

조회 수: 5 (최근 30일)
Leonid Guerchkovitch
Leonid Guerchkovitch 2021년 11월 24일
댓글: Paul 2021년 11월 24일
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.

채택된 답변

Paul 2021년 11월 24일
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.
  댓글 수: 4
Leonid Guerchkovitch
Leonid Guerchkovitch 2021년 11월 24일
편집: Leonid Guerchkovitch 2021년 11월 24일
Thank you Paul. Tech support from Mathworks suggested to use this line of code below as it avoid the issue with the name conflict. Seems to work well from initial testing.
RepNames = replace_block(gain_blocks{i},'Gain','do178Lib/Simulink/Math Operations/Gain','noprompt');
Paul 2021년 11월 24일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)


Help CenterFile Exchange에서 Simulink Functions에 대해 자세히 알아보기




Community Treasure Hunt

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

Start Hunting!

Translated by