Copying a subsystem from one model to another
Show older comments
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
More Answers (1)
Fangjun Jiang
on 6 Sep 2011
2 votes
It can be done although it takes some coding.
- Use add_block(SubSysBlockInOriginalModel,Destination) to copy the Subsystem block
- Use C=get_param(SubSysBlockInOriginalModel,'PortConnectivity')
- 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.
- 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.
- 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
Michael Joslin
on 6 Sep 2011
Fangjun Jiang
on 6 Sep 2011
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']);
Michael Joslin
on 7 Sep 2011
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.
Michael Joslin
on 7 Sep 2011
Euan Russano
on 5 Jan 2015
How? I am having the same problem...
Vishal Varadraj
on 24 Jun 2022
Could you please help me out in structuring the for loop. I am having trouble understanding the parameters. Thanks!!
CHEN
on 23 Jun 2026
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
Categories
Find more on Subsystems in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!