MATLAB Answers

Copying a subsystem from one model to another

260 views (last 30 days)
I need to copy a subsystem and all of the blocks attached to the input and output ports of it to a new model for modification. This needs to be done all in script because I am trying to automate a RCP process. The subsystem in question is in a layer with several other subsystems in it, and all of these belong to a higher parent system. Any ideas on how i can target the desired section of block diagram and copy it to a new model?

  0 Comments

Sign in to comment.

Accepted Answer

Guy Rouleau
Guy Rouleau on 6 Sep 2011
Fangjun's solution is possible, but this is probably simpler with function like:
Simulink.SubSystem.copyContentsToBlockDiagram
and
Simulink.BlockDiagram.copyContentsToSubSystem

  5 Comments

Show 2 older comments
Guy Rouleau
Guy Rouleau on 8 Sep 2011
Fangjun, I am not sure I understand your use case. If you have a good reason to want a specific enhancement, I recommend contacting technical support to explain your use case and the sort of functionality you are looking for.
Fangjun Jiang
Fangjun Jiang on 8 Sep 2011
Guy, this is not my use case. This is the OP's question. The OP wants to copy not only the subsystem block, but also copy "all of the blocks attached to the input and output ports of it to a new model". I certainly can see the need for such a use case.
What you suggested is not that much different than using add_block(). The key to solve this problem is to find all the connected blocks outside of the subsystem block and copy them. I am not convinced that the problem is solvable using just the two methods mentioned in your answer.
Guy Rouleau
Guy Rouleau on 9 Sep 2011
Now I see... I agree that this will require some find_system and copy_block, add_line, etc, to find all the connected blocks outside of the subsystem block and copy them.

Sign in to comment.

More Answers (1)

Fangjun Jiang
Fangjun Jiang on 6 Sep 2011
It can be done although it takes some coding.
  1. Use add_block(SubSysBlockInOriginalModel,Destination) to copy the Subsystem block
  2. Use C=get_param(SubSysBlockInOriginalModel,'PortConnectivity')
  3. C is a structure array, check the meaning of its field from Simulink document or http://www.mathworks.com/help/toolbox/simulink/slref/f23-7517.html.
  4. Go through a loop. For Inports, find the SrcBlock handle, use add_block() to add the block, use add_line() to add the signal connection. For Outports, find the DstBlock handle or handles because the outport may connect to more than one blocks. Go through another loop to add each destination block and signal line.
  5. Try it and it should work out. add_block works on block handle too. You can use the same block name in the new model. add_line() may need a little work if you never used it before. Ask again or comment further if you need help.

  6 Comments

Show 3 older comments
Fangjun Jiang
Fangjun Jiang on 7 Sep 2011
What is the variable untitled? Are your supposed to do add_block('testModel/Subsystem1','untitled/target']) unless you defined untitled='untitled'?
Run the code above in my comment. Did it copy the block? This is the same technique as in your previous two questions. I thought you've already knew it.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!