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

조회 수: 2 (최근 30일)
Vandhana
Vandhana 2018년 4월 23일
댓글: Kiran Kannan 2020년 4월 24일
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.

채택된 답변

TAB
TAB 2018년 4월 27일
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
Kiran Kannan
Kiran Kannan 2020년 4월 24일
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');

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Subsystems에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by