Copying a subsystem from one model to another

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?

 Accepted Answer

Fangjun's solution is possible, but this is probably simpler with function like:
Simulink.SubSystem.copyContentsToBlockDiagram
and
Simulink.BlockDiagram.copyContentsToSubSystem

5 Comments

Thanks, Guy! I need to learn the new OO methods.
What about copying all the lines and blocks that are connect to the Inports and Outports of the subsystem block? I suspect there is no readily available function for it. It probably needs to go through a similar loop.
Thanks a lot! This is really helpful.
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.
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.
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)

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.

8 Comments

Whenever I try the add_block(Source, Destination) I keep getting the following error, ??? A new block named 'Destination' cannot be added. Destination is the name of the model I'm trying to copy the system to.
Both Source and Destination should be strings specifying the full path of the block. Try this;
f14;
NewModel=new_system;open_system(NewModel);
NewModel=get_param(NewModel,'Name');
add_block('f14/Controller',[NewModel,'/Controller']);
I have a model testModel with Subsystem1 that I tried to copy to a new empty model untitlted using the command:
add_block('testModel/Subsystem1',[untitled,'/target'])
I also tried adding an empty subsystem named target and tried the command again but I keep getting this error,
??? Block diagram 'untitled' contains no blocks or all blocks are virtual.
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.
Nevermind I solved it.
How? I am having the same problem...
Could you please help me out in structuring the for loop. I am having trouble understanding the parameters. Thanks!!
This is the code I write with Jiang's insight, thanks Jiang!
function copyBlockAndConnections(dstModelName,srcSubSysPath,dstSubSysPath,posOffset)
% 获取模块句柄
blockHandle = getSimulinkBlockHandle(srcSubSysPath, true);
% 获取模块的所有端口
if blockHandle == -1
disp('没找到模块');
return;
end
add_block(srcSubSysPath,dstSubSysPath)
adjustPos(srcSubSysPath,dstSubSysPath,posOffset);
h2 = get_param(dstSubSysPath,'PortHandles');
ports = get_param(blockHandle, 'PortHandles');
% 处理输入端口
for i = 1:length(ports.Inport)
lineHandle = get_param(ports.Inport(i), 'Line');
if lineHandle ~= -1
srcMdl_srcPort = get_param(lineHandle, 'SrcPortHandle');
if srcMdl_srcPort ~= -1
srcMdl_srcBlock = get_param(srcMdl_srcPort, 'Parent');
blockType = get_param(srcMdl_srcBlock, 'BlockType');
blkName = get_param(srcMdl_srcBlock, "Name");
dstMdl_SrcBlock = dstModelName + "/" + blkName;
if strcmp(blockType, 'Inport') || ...
strcmp(blockType, 'Goto') || ...
strcmp(blockType, 'From')
add_block(srcMdl_srcBlock,dstMdl_SrcBlock,'MakeNameUnique','on');
h1 = get_param(dstMdl_SrcBlock,'PortHandles');
add_line(dstModelName,h1.Outport(1),h2.Inport(i));
adjustPos(srcMdl_srcBlock,dstMdl_SrcBlock,posOffset);
end
end
end
end
% 处理输出端口
for i = 1:length(ports.Outport)
lineHandle = get_param(ports.Outport(i), 'Line');
if lineHandle ~= -1
srcMdl_dstPorts = get_param(lineHandle, 'DstPortHandle');
for j = 1:length(srcMdl_dstPorts)
if srcMdl_dstPorts(j) ~= -1
srcMdl_dstBlock = get_param(srcMdl_dstPorts(j), 'Parent');
blockType = get_param(srcMdl_dstBlock, 'BlockType');
blkName = get_param(srcMdl_dstBlock, "Name");
dstMdl_dstBlock = dstModelName + "/" + blkName;
if strcmp(blockType, 'Outport') || ...
strcmp(blockType, 'Goto') || ...
strcmp(blockType, 'From')
add_block(srcMdl_dstBlock,dstMdl_dstBlock,'MakeNameUnique','on');
h1 = get_param(dstMdl_dstBlock,'PortHandles');
add_line(dstModelName,h2.Outport(i),h1.Inport(1));
adjustPos(srcMdl_dstBlock,dstMdl_dstBlock,posOffset);
end
end
end
end
end
end
function adjustPos(srcSubSysPath,dstSubSysPath,posOffset)
srcPos = get_param(srcSubSysPath,'position');
newPos = srcPos + posOffset;
set_param(dstSubSysPath,'position',newPos)
end

Sign in to comment.

Tags

Asked:

on 6 Sep 2011

Commented:

on 23 Jun 2026

Community Treasure Hunt

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

Start Hunting!