How to replace a single Simulink block with self-modifying mask block with same interface, maintaining all connections

15 vues (au cours des 30 derniers jours)
I am using MATLAB R2017a, and I'd like to replace a single block (I have its handle as block_handle) with another block (I have its path in a library). The new block is a self-modfying mask, and its parameters need to be set so that the new block has the same interface (number of inports and outports) as the existing block. I would like to retain all the connections.
I can do
replace_block(sys, 'Handle', block_handle, new_block_lib_path, 'noprompt');
But the problem is that because the default mask parameter does not correspond to the correct value, the block does not fit. So my solution is to first do
new_block_temp = add_block(new_block_lib_path, [sys '/' 'temp'], 'MakeNameUnique', 'on');
and setup the mask parameters for the new block, before finally doing
replace_block(sys, 'Handle', block_handle, getfullname(new_block_temp), 'noprompt');
The problem is that the new (replacement) blocks are shifted relative to the existing blocks, and all the connections are broken.
I tried to hand-roll my own version of "replace_block", which would remove the need to add the temporary block. My solution works as long as all lines in the diagram have SegmentType as 'trunk'. However, when the diagram has lines with SegmentType as 'branch', my solution breaks.
I don't see SegmentType documented anywhere, and would like to avoid the complexity of dealing with creating new lines to begin with. Note that the parameters of a line such as DstPortHandle are read-only, and so it does not appear to be possible to mutate an existing line to alter its connectivity.
  4 commentaires
Fangjun Jiang
Fangjun Jiang le 3 Nov 2022
Best way is to attach an example, if you could...
Did you try manually first?
Another way to try. add new block, set parameter, read old block position, delete old block, set new block position.
Gustavo
Gustavo le 4 Nov 2022
I've attached the example files. In summary, the example works as I expect when running test_support.m with replace_with_built_in = true; and does not work when running with replace_with_built_in = false;
bar.mdl original
bar.mdl after replace_with_built_in = true;
bar.mdl after replace_with_built_in = false;
lib_foo.slx:

Connectez-vous pour commenter.

Réponses (1)

Fangjun Jiang
Fangjun Jiang le 4 Nov 2022
I was able to duplicate your problem. I don't know why. Need more time to figure it out.
But I did find an alternative. Basicaly not to use replace_block(). Instead, delete the old block, set the new block position.
In effect, it costs the same three lines of code.
% % % set size after updating mask parameter. Move it away from the diagram
% % % for clarity
% % set_param(new_block_temp, 'Position', get_param(block, 'Position') - [0 1000 0 1000])
% %
% % new_block = replace_block(sys, 'Handle', block_handle, getfullname(new_block_temp), 'noprompt');
% %
% % %delete_block(block) % this block gets replaced.
% % delete_block(new_block_temp)
Pos=get_param(block, 'Position');
delete_block(block);
set_param(new_block_temp,'Position',Pos);
  5 commentaires
Gustavo
Gustavo le 7 Nov 2022
I understand the problem to be with my mask initialization function. I thought it was acceptable to re-create the model from scratch each time the initialization runs, but it is not. If the mask parameter inputs have not changed, then the model should not change either (not even by deleting and re-creating the exact same model, because the blocks have different identities.)
Fangjun Jiang
Fangjun Jiang le 9 Nov 2022
Yes. I was able to duplicate the problem. Yes. The problem was caused by the mask initialization.
If a subsystem block is currently fully connected and you delete an Inport or Outport block inside, the connecting line for that port would immediately broke. You could see this easily when you do it manually any time.
In your mask initialization, you delteted all the contents inside the subsystem.
You will probably need to re-consider your approach.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Subsystems dans Help Center et File Exchange

Tags

Produits


Version

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by