Programmatically Moving Simulink Blocks Between Subsystems

23 views (last 30 days)
Hey everyone,
I've been working on a project involving Simulink, and I've encountered a bit of a puzzle that I'm hoping someone here might be able to help me solve.
I'm looking to programmatically cut a Simulink block from one subsystem and paste it into another. Now, I know there are functions like Simulink.BlockDiagram.copyContentsToSubsystem, but here's the problem: I need to preserve the Simulink ID (SID) of the block. Simply copying and pasting won't work because of this requirement.
My goal is to implement a deleteFcn callback for all blocks in my project. Instead of outright deleting a block, I want to move it to a designated "recycling bin" subsystem. This way, I can maintain the SID and have a sort of history of the blocks that were removed.
Does anyone have any experience or insights into how I might achieve this? I've been digging through the Simulink documentation, but I haven't found a straightforward solution yet.
Any help or pointers in the right direction would be greatly appreciated!
Thanks in advance.
  1 Comment
Fangjun Jiang
Fangjun Jiang on 28 Mar 2024 at 19:08
Simulink.ID.getSID is not recommended. Use handles, paths, or objects instead of Simulink® Identifiers. For more information, see Compatibility Considerations.

Sign in to comment.

Answers (1)

Divyanshu
Divyanshu on 1 Apr 2024 at 8:23
Hi Maximilian,
It is possible to programmatically cut a block from one subsystem and paste it inside another subsystem of a model. However preserving the original ID of the block and re-using the same after moving the block is difficult.
When you cut & paste the same block in the canvas of Simulink, Simulink assignes a new Identifier to the same block which would be different from the previous one. Here is a possible workaround:
  • Maintain a Dictionary with key as the Name of the Block and Value as the current Unique Identifier of the block.
  • The Unique Id can be determined using function 'getSimulinkBlockHandle'.
  • Whenever you move any block from one subsystem to other just make an entry in the dictionary.
  • This dictionary would keep a record of all the moved blocks.
Please refer the following documentations for further details:
Hope it helps!
  1 Comment
Fangjun Jiang
Fangjun Jiang on 1 Apr 2024 at 13:24
Edited: Fangjun Jiang on 1 Apr 2024 at 13:29
Although using Simulink ID (Simulink.ID.getSID) is not recommended, I have tried the following.
When doing it manually (interactively), "Cut and Paste" will keep the same Simulink ID, but "Copy and Paste" will not.
I have not been able to find a way to do "Cut and Paste" programmingly, which is what the OP is asking for.
I do not understand how Dictioary can help on this. If only for keeping a record of the change history, a Revision Control Software can do a better job. Simulink now has built-in support for RCS, such as Git.

Sign in to comment.

Categories

Find more on Large-Scale Modeling in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!