Connect line using add_line command to same port connections
Show older comments
While I tried to collect lines in paralel, an error popup:
>> Battery_Array_tmp
Error using Battery_Array_tmp (line 33)
The first port already has a line connection
%%% connect minus to plus ports:
if v>1
add_line(mdl,PH_Add_MC_cube{v-1,i}.LConn(2),PH_Add_MC_cube{v,i}.LConn(2),'Autorouting','on');
end
Can someone assist to solve it?
I adding part of rellevant script:
%%%% Scriptto creat top design - withot Batteries/
%%%% only to speedup simulation time during debug Block connections
open_system('Battery_arc_tmp')
mdl = 'Battery_arc_tmp';
battery_model = find_system(mdl,'FindAll','on','Name','Battery_arc_tmp');
numRows = 3;
% Get handle to existing (external) POS and NEG ports
Plus_MC = find_system(mdl,'LookUnderMasks','All','FindAll','on','Name','POS');
Minus_MC = find_system(mdl,'LookUnderMasks','All','FindAll','on','Name','NEG');
%%% add MC_cube - basic String_unit:
for i=1:2 %% set two columns
colPos = 500; %% spaces between columns
for v=1:numRows %% loop for 5 String per column
nl=num2str(v + numRows*(i-1));
if i==1
% Add_MC_cube(v) = add_block('MC_Cube/STRING_MC_Cube 1', [mdl,'/STRING_MC_Cube ',nl]);
% % % % % % MC_TMP empty Submodule - only for quick compilation....
Add_MC_cube(v) = add_block('MC_TMP/STRING_MC_Cube 1', [mdl,'/STRING_MC_Cube ',nl]);
else
% Add_MC_cube(v) = add_block('MC_Cube2/STRING_MC_Cube 1', [mdl,'/STRING_MC_Cube ',nl]);
% % % % % % MC_TMP empty Submodule - only for quick compilation....
Add_MC_cube(v) = add_block('MC_TMP2/STRING_MC_Cube 1', [mdl,'/STRING_MC_Cube ',nl]);
end
posc = get(Add_MC_cube(v),'Position');
set(Add_MC_cube(v),'Position',posc + [100+(i-1)*colPos 100*(v-1)+45 100+(i-1)*colPos 100*(v-1)+45])
PH_Add_MC_cube{v,i}=get(Add_MC_cube(v),'PortHandles');
%%% connect minus to plus ports:
if v>1
add_line(mdl,PH_Add_MC_cube{v-1,i}.LConn(2),PH_Add_MC_cube{v,i}.LConn(2),'Autorouting','on');
end
end
end
while connecting 2 module there is no problem/
But when increase above 2 module i gos an error message (above)..

please, see pic attached...

Thank for assist.
Tommy
1 Comment
Answers (1)
Altaïr
on 14 Apr 2025
You're on the right track. Here are a couple of points to consider from the approach being used. The current script connects only the second connection ports of blocks in a given column:
%%% connect minus to plus ports:
if v>1
add_line(mdl,PH_Add_MC_cube{v-1,i}.LConn(2),PH_Add_MC_cube{v,i}.LConn(2),'Autorouting','on');
end
To improve this, replace it with the following lines to connect the first connection ports into one group and the second connection ports into another group:
%%% connect minus and plus ports into two groups:
if v>1
add_line(mdl,PH_Add_MC_cube{v-1,i}.LConn(1),PH_Add_MC_cube{v,i}.LConn(1),'Autorouting','smart');
add_line(mdl,PH_Add_MC_cube{v-1,i}.LConn(2),PH_Add_MC_cube{v,i}.LConn(2),'Autorouting','smart');
end
Setting 'Autorouting' to smart helps in better routing of the lines. To connect these groups to the external port, simply add a connection from the external ports to the first port of each column using the lines below:
% connect the external NEG port to NEG port of STRING_MC_Cube at (ro1,col)=(1,1):
PH_Minus_MC=get(Minus_MC,'PortHandles');
add_line(mdl,PH_Minus_MC.RConn,PH_Add_MC_cube{1,1}.LConn(1), 'Autorouting','smart');
% connect the external POS port to POS port of STRING_MC_Cube at (ro1,col)=(1,2):
PH_Plus_MC=get(Plus_MC,'PortHandles');
add_line(mdl,PH_Plus_MC.RConn,PH_Add_MC_cube{1,2}.LConn(1), 'Autorouting','smart');
This approach aligns with the response to another query for consistency:
The resulting model should match expectations. I hope this meets your need!

8 Comments
Tommy
on 15 Apr 2025
Tommy
on 15 Apr 2025
Tommy
on 15 Apr 2025
Altaïr
on 15 Apr 2025
Seems to work fine from my end. I've attached the dummy slx files I'm using to check the code. Please confirm if the actual files are similar to the ones attached or you can simple attach your 'MC_TMP', 'MC_TMP2' and 'Battery_arc_tmp' files.
Tommy
on 15 Apr 2025
I am unable to make the specified connection manually too, using the shared models. The issue seems to occur when 'STRING_MC_Cube 1' in 'MC_TMP' and 'MC_TMP2' is empty. Could you try running the script after filling 'STRING_MC_Cube 1' with its contents?
Tommy
on 15 Apr 2025
Categories
Find more on General Applications 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!


