Reputation: 222
I am searching for a way to copy the content of one Subsystem to another existing Subsystem in the same model via code. Is there a best practise?
Thank you
Upvotes: 0
Views: 2999
Reputation: 31
This is not so difficult but little tricky. You can copy the contents of your subsystem to a new block diagram first (temporary destination) using Simulink.SubSystem.copyContentsToBlockDiagram. You should also use save_system command.
Then from this temporary destination file you can copy the contents to the desired subsystem in final destination block diagram using Simulink.BlockDiagram.copyContentsToSubsystem. Then you can use commands like save_system and close_system. This retains whatever SIL/PIL 'test harnesses' you have created.
Upvotes: 3
Reputation: 7006
You can use add_block
If your Model is called ModelSource
and the subsystem you want to copy is called Subsystem1
and you are creating a copy to the same model, you can copy that subsystem using the command
add_block('ModelSource/Subsystem1','ModelSource/Subsystem1','MakeNameUnique','on')
The "MakeNameUnique" will ensure that your new block has a unique name.
Note that the new block will have the same coordinates as the old block. You can move the block down by doing
coords = get_param(gcb,'Position');
coords(2) = coords(2) + 50;
coords(4) = coords(4) + 50;
set_param(gcb,'Position',coords);
Which will move the block 50 pixels down from the location of the source block.
Upvotes: 2