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

2 次查看(过去 30 天)
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