Removing path name from Simulink block name

5 次查看(过去 30 天)
Hello,
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
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
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
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 中查找有关 Programmatic Model Editing 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by