How to copy level 1 inport and outport blocks to top most parent level

3 views (last 30 days)
I need to copy level 1 inport and outport blocks to top most parent level. How to copy in/outport blocks and connect to respective input and output ports.
Please suggest.

Accepted Answer

TAB
TAB on 27 Apr 2018
You can use following function. It works for inport block. It routes the inport block from a subsystem to route level. You can easily modify the code to make it work for outport block.
function MEditRoutePortToRootLevel(argInportH)
% Local assignment
inPBlkH = argInportH;
% Collect root model name
rootMdlH = bdroot(inPBlkH);
rootMdlName = get_param(rootMdlH, 'Name');
% Collect original inport info
inPBlkPar = get_param(inPBlkH, 'Parent');
% Loop to copy the inport block to parent level untill model root is
% reached
isParRoot = strcmp(inPBlkPar, rootMdlName);
while(~isParRoot)
% Collect inport block and its parent info
inPIdx = get_param(inPBlkH, 'Port');
inPBlkName = get_param(inPBlkH, 'Name');
inPBlkPar = get_param(inPBlkH, 'Parent');
inPBlkParH = get_param(inPBlkPar, 'handle');
inPBlkPath = [inPBlkPar '/' inPBlkName];
inPBlkParPar = get_param(inPBlkParH, 'Parent');
% Collect the corresponding port position on parent subsystem
parPortH = get_param(inPBlkParH, 'PortHandles');
pos = get_param(parPortH.Inport(str2double(inPIdx)), 'Position');
% Decide new block position
pX_OFFSET = 60;
pW = 30;
pH = 14;
pX = pos(1)- pX_OFFSET;
pY = pos(2)-(pH/2);
% Decide src and dst blocks to copy
srcBlk = inPBlkPath;
dstBlk = [inPBlkParPar '/' inPBlkName];
% Copy the inport block to subsystem parent level and add connection
newBlkH = add_block(srcBlk, dstBlk, 'Position', [pX, pY, pX+pW, pY+pH]);
add_line(inPBlkParPar, [inPBlkName '/1'], [inPBlkParSysName '/' inPIdx]);
% Check if new block is at root level. If not then loop again
inPBlkH = newBlkH;
inPBlkPar = get_param(inPBlkH, 'Parent');
isParRoot = strcmp(inPBlkPar, rootMdlName);
end
  1 Comment
Kiran Kannan
Kiran Kannan on 24 Apr 2020
This works great!
'inPBlkParSysName' is undefined in code. But this can be defined as shown below in the '% Collect inport block and its parent info' section:
inPBlkParSysName = get_param(inPBlkParH, 'Name');

Sign in to comment.

More Answers (0)

Categories

Find more on Simulink Functions in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!