Programmatically Moving Simulink Blocks Between Subsystems

조회 수: 24 (최근 30일)
Maximilian Panzik
Maximilian Panzik 2024년 3월 28일
편집: Fangjun Jiang 2024년 4월 1일
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
Fangjun Jiang
Fangjun Jiang 2024년 3월 28일
Simulink.ID.getSID is not recommended. Use handles, paths, or objects instead of Simulink® Identifiers. For more information, see Compatibility Considerations.

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

답변 (1개)

Divyanshu
Divyanshu 2024년 4월 1일
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
Fangjun Jiang
Fangjun Jiang 2024년 4월 1일
편집: Fangjun Jiang 2024년 4월 1일
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.

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

카테고리

Help CenterFile Exchange에서 Interactive Model Editing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by