Removing path name from Simulink block name

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.

 Accepted Answer

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 Comments

Hi Paul,
Thank you for the detailed and thoughtful answer.
You're absolutely right, I'm trying to replace blocks in an automated way while keeping their original values (gain value, integrator settings, etc). The issue I'm running into is that the replace_block command does not work well when there mutliple blocks of the same type and the same name in different subsystems. I've added my section of the script to illustrate.
match = wildcardPattern + "/"; % Needed later for block name stripping
gain_blocks = getfullname(Simulink.findBlocksOfType(model,'Gain')); % Generate list of gain blocks in model
ngain = length(gain_blocks);
for i =1:ngain
dp = get_param(char(gain_blocks(i,1)), 'DialogParameters'); % Dialog parameters for gain blocks
fn = fieldnames(dp); % Field Names
for j = 1:length(fn)
storage(j,:) = cellstr(get_param(gain_blocks(i,1), fn{j})); % Storing the Values
end
block_name = erase(gain_blocks(i,1),match); % block name manipulation
RepNames = replace_block(model,'Name',char(block_name),'do178Lib/Simulink/Math Operations/Gain','noprompt'); % Replacement with DO-178 Block
for k = 1:length(fn)
set_param(char(gain_blocks(i,1)), char(fn{k}),char(storage(k,:))); % Restoration of original values and settings
end
end
Have you engaged with Tech Support on this issue? I'm really surprised that you have to use the erase() command.
As a workaround maybe try this:
After you store all the block parameters in storage, you can replace one parameter, like 'Gain' with some unique value, like xyzzy.
set_param(gain_blocks{i,1},'Gain','xyzzy')
Then instead of doing replace_block based on 'Name' , do
replace_block(model,gain_blocks{i,1},'Gain','xyzzy','do178Lib/Simulink/Math Operations/Gain','noprompt');
Alternatively and similarly, you can do
set_param(gain_blocks{i,1},'UserData','xyzzy');
and then do the replace_block() based on the UserData Name/Value pair. I assume you're not already using UserData because you're not copying it over.
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');
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)

Categories

Products

Release

R2020b

Asked:

on 24 Nov 2021

Commented:

on 24 Nov 2021

Community Treasure Hunt

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

Start Hunting!