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

2 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

TAB
TAB el 27 de Abr. de 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 comentario
Kiran Kannan
Kiran Kannan el 24 de Abr. de 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');

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Subsystems en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by