add_line connection for to column cells

Hi there,
While creating column Cells connections, last cell in each column still not connected...stayed unconnected.
I wonder if someone can assist to solve this problem.
please see shared Matlab code (file.m) and picture (marked in red line)
Thanks for help
Tommy
open_system('Module_arc')
mdl = 'Module_arc';
bat_rec_model = find_system(mdl,'FindAll','on','Name','Module_arc');
%%% add Cell - basic CELL_unit:
for i=1:2 %% set two columns
colPos = 200; %% spaces between columns
for v=1:4 %% loop for 13 cells per column
nl=num2str(v + 4*(i-1));
if i==1
AddCell(v) = add_block('CELL_Unit/CELL 1', [mdl,'/CELL ',nl]);
else
AddCell(v) = add_block('CELL_Unit2/CELL 1', [mdl,'/CELL ',nl]);
end
posc = get(AddCell(v),'Position');
set(AddCell(v),'Position',posc + [100+(i-1)*colPos 120*(v-1)-45 100+(i-1)*colPos 120*(v-1)-45])
PH_AddCell{v}=get(AddCell(v),'PortHandles');
%%% connect minus to plus ports:
if v>1
add_line(mdl,PH_AddCell{v-1}.LConn(2),PH_AddCell{v}.LConn(1),'Autorouting','on');
end
end
switch i
case 2
Minus_2_Cell = find_system(mdl,'LookUnderMasks','All','FindAll','on','Name','NEG');
PH_minus2Cell=get(Minus_2_Cell,'PortHandles');
Neg_port= add_line(mdl,PH_minus2Cell.RConn,PH_AddCell{1}.LConn(1),'Autorouting','on');
case 1
Plus_2_Cell = find_system(mdl,'LookUnderMasks','All','FindAll','on','Name','POS');
PH_plus2Cell=get(Plus_2_Cell,'PortHandles');
Pos_port= add_line(mdl,PH_plus2Cell.RConn,PH_AddCell{1}.LConn(1), 'Autorouting','on');
end
end

3 Comments

What is Module_arc? I tried running the posted code in MATLAB Online but got the error "Unable to find system or file 'Module_arc'."
Hi,
Module_arc.slx is just blanc canvas with 2 PCM_port:
please, see attached CELL.slx

Sign in to comment.

 Accepted Answer

Altaïr
Altaïr on 21 Mar 2025
Edited: Altaïr on 21 Mar 2025
Hey @Tommy,
Similar to your question at https://www.mathworks.com/matlabcentral/answers/2174846, I would like to modify my answer present there with the file you have attached.
The line PH_AddCell{v}=get(AddCell(v),'PortHandles'); in the code only retains port handles for blocks in the current column, which gets replaced with each iteration as i changes. To maintain a matrix of handles, the following approach can be used:
PH_AddCell{v,i}=get(AddCell(v),'PortHandles');
This method ensures that handles to all the blocks are retained by the end of the nested loop, allowing for additional connections to POS and NEG ports, as well as the connection between CELL 4 and CELL 8, using their respective handles. Here's the complete code with the final result:
open_system('Module_arc')
mdl = 'Module_arc';
bat_rec_model = find_system(mdl,'FindAll','on','Name','Module_arc');
numRows = 4;
%%% add Cell - basic CELL_unit:
for i=1:2 %% set two columns
colPos = 200; %% spaces between columns
for v=1:numRows %% loop for 13 cells per column
nl=num2str(v + numRows*(i-1));
if i==1
AddCell(v) = add_block('CELL_Unit/CELL 1', [mdl,'/CELL ',nl]);
else
AddCell(v) = add_block('CELL_Unit2/CELL 1', [mdl,'/CELL ',nl]);
end
posc = get(AddCell(v),'Position');
set(AddCell(v),'Position',posc + [100+(i-1)*colPos 120*(v-1)-45 100+(i-1)*colPos 120*(v-1)-45])
PH_AddCell{v,i}=get(AddCell(v),'PortHandles');
%%% connect minus to plus ports:
if v>1
add_line(mdl,PH_AddCell{v-1,i}.LConn(2),PH_AddCell{v,i}.LConn(1),'Autorouting','on');
end
end
end
% connect the POS port to first CELL (Cell 1):
Plus_2_Cell = find_system(mdl,'LookUnderMasks','All','FindAll','on','Name','POS');
PH_plus2Cell=get(Plus_2_Cell,'PortHandles');
Pos_port= add_line(mdl,PH_plus2Cell.RConn,PH_AddCell{1,1}.LConn(1,1), 'Autorouting','on');
% connect the NEG port to last CELL (Cell 8):
Minus_2_Cell = find_system(mdl,'LookUnderMasks','All','FindAll','on','Name','NEG');
PH_minus2Cell=get(Minus_2_Cell,'PortHandles');
Neg_port= add_line(mdl,PH_minus2Cell.RConn,PH_AddCell{1,2}.LConn(1),'Autorouting','on');
% connect positive and negative of the two cells in last row
CELL_4_8_connect = add_line(mdl,PH_AddCell{numRows,1}.LConn(1,2),PH_AddCell{numRows,2}.LConn(1,2), 'Autorouting','on');
For a quick start guide on programmatically modeling, visit:

9 Comments

graet,
it realy works....:-)
thanks a lot
Hi,
while I tried to increase cells, above 10 per row, an error pop up and I can't find out why.
can you assit to solve it?
Adding whole code (file.m):
open_system('Module_arc')
mdl = 'Module_arc';
bat_rec_model = find_system(mdl,'FindAll','on','Name','Module_arc');
numRows = 10;
%%% add Cell - basic CELL_unit:
for i=1:2 %% set two columns
colPos = 400; %% spaces between columns
for v=1:numRows %% loop for 13 cells per column
nl=num2str(v + numRows*(i-1));
if i==1
AddCell(v) = add_block('CELL_Unit/CELL 1', [mdl,'/CELL ',nl]);
else
AddCell(v) = add_block('CELL_Unit2/CELL 1', [mdl,'/CELL ',nl]);
end
posc = get(AddCell(v),'Position');
set(AddCell(v),'Position',posc + [100+(i-1)*colPos 120*(v-1)-45 100+(i-1)*colPos 120*(v-1)-45])
PH_AddCell{v,i}=get(AddCell(v),'PortHandles');
%%% connect minus to plus ports:
if v>1
add_line(mdl,PH_AddCell{v-1,i}.LConn(2),PH_AddCell{v,i}.LConn(1),'Autorouting','on');
end
end
end
% connect the POS port to first CELL (Cell 1):
Plus_2_Cell = find_system(mdl,'LookUnderMasks','All','FindAll','on','Name','POS');
PH_plus2Cell=get(Plus_2_Cell,'PortHandles');
Pos_port= add_line(mdl,PH_plus2Cell.RConn,PH_AddCell{1,1}.LConn(1,1), 'Autorouting','on');
% connect the NEG port to last CELL (Cell 8):
Minus_2_Cell = find_system(mdl,'LookUnderMasks','All','FindAll','on','Name','NEG');
PH_minus2Cell=get(Minus_2_Cell,'PortHandles');
Neg_port= add_line(mdl,PH_minus2Cell.RConn,PH_AddCell{1,2}.LConn(1),'Autorouting','on');
% connect positive and negative of the two cells in last row
CELL_4_8_connect = add_line(mdl,PH_AddCell{numRows,1}.LConn(1,2),PH_AddCell{numRows,2}.LConn(1,2), 'Autorouting','smart');
no_cells=10;
for l=1:no_cells
nl=num2str(l);
%%% add goto_1 - input port signal:
AddGoto1(l)=add_block('simulink/Signal Routing/Goto', [mdl,'/gmod_',nl]);
posg1 = get(AddGoto1(l),'Position');
set(AddGoto1(l),'Position',posg1 + [530 120*(l-1)-205 550 120*(l-1)-205])
set(AddGoto1(l),'GotoTag',['LFT_MES',nl]) %% - set tag
%%% add goto_11 - input port signal:
AddGoto2(l)=add_block('simulink/Signal Routing/Goto', [mdl,'/gmod_1',nl]);
posg3 = get(AddGoto2(l),'Position');
set(AddGoto2(l),'Position',posg3 + [690 120*(l-1)-205 710 120*(l-1)-205])
set(AddGoto2(l),'GotoTag',['RGT_MES',nl]) %% - set tag
set_param(AddGoto2(l),'Orientation','left')
AddFrom1(l)=add_block('simulink/Signal Routing/From', [mdl,'/fmod_',nl]);
posf = get(AddFrom1(l),'Position');
set(AddFrom1(l),'Position',posf + [1200 70*(l-1)-100 1220 70*(l-1)-100])
set(AddFrom1(l),'GotoTag',['RGT_MES',nl]) %% - set tag
AddFrom2(l)=add_block('simulink/Signal Routing/From', [mdl,'/fmod_1',nl]);
posf = get(AddFrom2(l),'Position');
set(AddFrom2(l),'Position',posf + [320 70*(l-1)-100 340 70*(l-1)-100])
set(AddFrom2(l),'GotoTag',['LFT_MES',nl]) %% - set tag
set_param(AddFrom2(l),'Orientation','left')
%%% get port handle
PH_AddGoto1{l}=get(AddGoto1(l),'PortHandles');
PH_AddGoto2{l}=get(AddGoto2(l),'PortHandles');
PH_AddFrom1{l}=get(AddFrom1(l),'PortHandles');
PH_AddFrom2{l}=get(AddFrom2(l),'PortHandles');
%%% connect Tags 'goto' with output CELLs - for each column:
add_line(mdl,PH_AddCell{l,1}.Outport,PH_AddGoto1{l}.Inport);
add_line(mdl,PH_AddCell{l,2}.Outport,PH_AddGoto2{l}.Inport);
end
% %%% Adding output Mux for enable batteries m mesurments :
Add_mux=add_block('simulink/Signal Routing/Mux',[mdl,'/mux']);
set(Add_mux,'Inputs',num2str(no_cells))
posf = get(Add_mux,'Position');
for z=1:no_cells
set(Add_mux,'Position',posf + [1400 150 1400 150+10*z])
end
PH_Add_mux=get(Add_mux,'PortHandles');
% %%% Adding output DMux for enable batteries m mesurments :
Add_mux2=add_block('simulink/Signal Routing/Mux',[mdl,'/mux2']);
set(Add_mux2,'Inputs',num2str(no_cells))
posf2 = get(Add_mux2,'Position');
for r2=1:no_cells
set(Add_mux2,'Position',posf2 + [140 150 140 150+10*r2])
end
PH_Add_mux2=get(Add_mux2,'PortHandles');
set_param(Add_mux2,'Orientation','left')
Add_in=add_block('built-in/Outport', [mdl,'/Messure_RGT']);
posg = get(Add_in,'Position');
set(Add_in,'Position',posg + [1510 280 1520 260])
PH_Add_out=get(Add_in,'PortHandles');
Add_in1=add_block('built-in/Outport', [mdl,'/Messure_LFT']);
posq = get(Add_in1,'Position');
set(Add_in1,'Position',posq + [50 280 60 260])
PH_Add_out2=get(Add_in1,'PortHandles');
set_param(Add_in1,'Orientation','left')
%%% connecting firs Demux(1xn) to Mux(2x1):
for u=1:no_cells
%%% connect AddFrom2 to Mux:
add_line(mdl,PH_AddFrom1{u}.Outport,PH_Add_mux.Inport(u));
add_line(mdl,PH_AddFrom2{u}.Outport,PH_Add_mux2.Inport(u));
end
add_line(mdl,PH_Add_mux.Outport,PH_Add_out.Inport);
add_line(mdl,PH_Add_mux2.Outport,PH_Add_out2.Inport);
Hi @Tommy,
I assume that the error similar to the following:
It seems like there might be an issue related to the naming style chosen for the Goto and From blocks, which can potentially lead to duplicate names. A solution is to use more distinct naming, such as gmod_1_1 and gmod_1_2 instead of gmod_1 and gmod_11. Similar adjustments should be made to the From blocks' names. Here's the modified second for-loop:
for l=1:no_cells
nl=num2str(l);
%%% add gmod_1_1 - input port signal:
AddGoto1(l)=add_block('simulink/Signal Routing/Goto', [mdl,'/gmod_',nl,'_1']);
posg1 = get(AddGoto1(l),'Position');
set(AddGoto1(l),'Position',posg1 + [530 120*(l-1)-205 550 120*(l-1)-205])
set(AddGoto1(l),'GotoTag',['LFT_MES',nl]) %% - set tag
%%% add gmod_1_2 - input port signal:
AddGoto2(l)=add_block('simulink/Signal Routing/Goto', [mdl,'/gmod_',nl,'_2']);
posg3 = get(AddGoto2(l),'Position');
set(AddGoto2(l),'Position',posg3 + [690 120*(l-1)-205 710 120*(l-1)-205])
set(AddGoto2(l),'GotoTag',['RGT_MES',nl]) %% - set tag
set_param(AddGoto2(l),'Orientation','left')
%%% add fmod_1_1 - input port signal:
AddFrom1(l)=add_block('simulink/Signal Routing/From', [mdl,'/fmod_',nl,'_1']);
posf = get(AddFrom1(l),'Position');
set(AddFrom1(l),'Position',posf + [1200 70*(l-1)-100 1220 70*(l-1)-100])
set(AddFrom1(l),'GotoTag',['RGT_MES',nl]) %% - set tag
%%% add fmod_1_2 - input port signal:
AddFrom2(l)=add_block('simulink/Signal Routing/From', [mdl,'/fmod_1',nl,'_2']);
posf = get(AddFrom2(l),'Position');
set(AddFrom2(l),'Position',posf + [320 70*(l-1)-100 340 70*(l-1)-100])
set(AddFrom2(l),'GotoTag',['LFT_MES',nl]) %% - set tag
set_param(AddFrom2(l),'Orientation','left')
%%% get port handle
PH_AddGoto1{l}=get(AddGoto1(l),'PortHandles');
PH_AddGoto2{l}=get(AddGoto2(l),'PortHandles');
PH_AddFrom1{l}=get(AddFrom1(l),'PortHandles');
PH_AddFrom2{l}=get(AddFrom2(l),'PortHandles');
%%% connect Tags 'goto' with output CELLs - for each column:
add_line(mdl,PH_AddCell{l,1}.Outport,PH_AddGoto1{l}.Inport);
add_line(mdl,PH_AddCell{l,2}.Outport,PH_AddGoto2{l}.Inport);
end
Note that the variables numRows and no_cells must be equal.
Hi @Ashok,
I just correct as you mentioned above and it works perfect :-)
for l=1:numRows
nl=num2str(l);
%%% add goto_1 - input port signal:
AddGoto1(l)=add_block('simulink/Signal Routing/Goto', [mdl,'/gmod_',nl,'_1']);
posg1 = get(AddGoto1(l),'Position');
set(AddGoto1(l),'Position',posg1 + [530 120*(l-1)-205 550 120*(l-1)-205])
set(AddGoto1(l),'GotoTag',['LFT_MES',nl]) %% - set tag
%%% add goto_11 - input port signal:
AddGoto2(l)=add_block('simulink/Signal Routing/Goto', [mdl,'/gmod_',nl,'_2']);
posg3 = get(AddGoto2(l),'Position');
set(AddGoto2(l),'Position',posg3 + [690 120*(l-1)-205 710 120*(l-1)-205])
set(AddGoto2(l),'GotoTag',['RGT_MES',nl]) %% - set tag
set_param(AddGoto2(l),'Orientation','left')
AddFrom1(l)=add_block('simulink/Signal Routing/From', [mdl,'/fmod_',nl,'_1']);
posf = get(AddFrom1(l),'Position');
set(AddFrom1(l),'Position',posf + [1200 70*(l-1)-100 1220 70*(l-1)-100])
set(AddFrom1(l),'GotoTag',['RGT_MES',nl]) %% - set tag
AddFrom2(l)=add_block('simulink/Signal Routing/From', [mdl,'/fmod_',nl,'_2']);
posf = get(AddFrom2(l),'Position');
set(AddFrom2(l),'Position',posf + [320 70*(l-1)-100 340 70*(l-1)-100])
set(AddFrom2(l),'GotoTag',['LFT_MES',nl]) %% - set tag
set_param(AddFrom2(l),'Orientation','left')
Thank you for your help
Can you assist for another problem?
I wrote a small script to made 4 modules and connection betweeen (pic attached).
I got error message while I tried to connect phisical POS and got "Dot indexing not support for variable of this type".
I don't understand what is the problem...
Matlab script also atteached:
open_system('String_arc')
mdl = 'String_arc';
Cube_model = find_system(mdl,'FindAll','on','Name','String_arc');
%%% add basic Module:
for v=1:4 %% loop for 4 Modules
nl=num2str(v);
Add_module(v) = add_block('Mod_26_cells/Module_MC 1', [mdl,'/Module_MC ',nl]);
posc = get(Add_module(v),'Position');
set(Add_module(v),'Position',posc + [100 120*(v-1)-45 100 120*(v-1)-45])
PH_Add_module{v}=get(Add_module(v),'PortHandles');
%%% connect minus to plus ports:
if v>1
add_line(mdl,PH_Add_module{v-1}.LConn(2),PH_Add_module{v}.LConn(1),'Autorouting','on');
end
end
% connect the POS port to first CELL (Cell 1):
Plus_str = find_system(mdl,'LookUnderMasks','All','FindAll','on','Name','POS');
PH_plus2str=get(Plus_str,'PortHandles');
add_line(mdl,Plus_str.RConn,PH_Add_module{1}.LConn(1), 'Autorouting','on');
Hi @Tommy,
It seems there might be a minor correction needed in the code. In the last line, it should be PH_plus2str.RConn instead of Plus_str.RConn.
Additionally, placing the following line after adding the Module_MC blocks will obtain the handles for all connection ports named POS (5 handles for the image mentioned):
Plus_str = find_system(mdl,'LookUnderMasks','All','FindAll','on','Name','POS');
Identifying the POS port which is an external connection could be challenging, so it might be beneficial to place this line before the for loop. This approach is also applicable for getting a handle to the external connection port named NEG.
Since there appears to be repetition of blocks, it might be advantageous to use model reference or subsystem reference to maintain a smaller top model size. More information on this can be found here:
I tried to change as you suggest, same error...
Can you modify my code for correct connection?
Thanks
open_system('String_arc')
mdl = 'String_arc';
Cube_model = find_system(mdl,'FindAll','on','Name','String_arc');
no_cells = 2;
%%% add basic Module:
for v=1:no_cells %% loop for 4 Modules
nl=num2str(v);
Add_module(v) = add_block('Mod_26_cells/Module_MC 1', [mdl,'/Module_MC ',nl]);
posc = get(Add_module(v),'Position');
set(Add_module(v),'Position',posc + [20 120*(v-1)+180 20 120*(v-1)+180])
PH_Add_module{v}=get(Add_module(v),'PortHandles');
%%% connect minus to plus ports:
if v>1
add_line(mdl,PH_Add_module{v-1}.LConn(2),PH_Add_module{v}.LConn(1),'Autorouting','on');
end
Plus_str = find_system(mdl, 'LookUnderMasks', 'All', 'FindAll', 'on', 'Name', 'POS');
PH_Plus2str=get(Plus_str,'PortHandles');
add_line(mdl,PH_Plus2str.RConn,PH_Add_module{2}.LConn(2), 'Autorouting','on');
end
Here's the modified code!
open_system('String_arc')
mdl = 'String_arc';
Cube_model = find_system(mdl,'FindAll','on','Name','String_arc');
no_cells = 2;
% Get handle to existing (external) POS and NEG ports
Plus_str = find_system(mdl,'LookUnderMasks','All','FindAll','on','Name','POS');
Minus_str = find_system(mdl,'LookUnderMasks','All','FindAll','on','Name','NEG');
%%% add basic Module:
for v=1:no_cells %% loop for no_cells Modules
nl=num2str(v);
Add_module(v) = add_block('Mod_26_cells/Module_MC 1', [mdl,'/Module_MC ',nl]);
posc = get(Add_module(v),'Position');
set(Add_module(v),'Position',posc + [20 120*(v-1)+180 20 120*(v-1)+180])
PH_Add_module{v}=get(Add_module(v),'PortHandles');
%%% connect minus to plus ports:
if v>1
add_line(mdl,PH_Add_module{v-1}.LConn(2),PH_Add_module{v}.LConn(1),'Autorouting','on');
end
end
% connect the external POS port to first CELL (Cell 1):
PH_plus2str=get(Plus_str,'PortHandles');
add_line(mdl,PH_plus2str.RConn,PH_Add_module{1}.LConn(1), 'Autorouting','on');
% connect the external NEG port to last CELL (Cell no_cells):
PH_minus2str=get(Minus_str,'PortHandles');
add_line(mdl,PH_minus2str.RConn,PH_Add_module{no_cells}.LConn(2), 'Autorouting','on');
And this is the resulting String_arc.slx model:
For no_cells = 2
For no_cells = 5
I have attached the String_arc.slx and Mod_26_cells.slx models I used as well.
Hi @Altaïr :-)
great!, works perfect.
Thanks a lot.
Tommy

Sign in to comment.

More Answers (1)

TED MOSBY
TED MOSBY on 21 Mar 2025
Edited: TED MOSBY on 21 Mar 2025
Hi Tommy,
The last cells are not connected as you want as shown in the image because you have not added it in your code. The key is simply:
  1. Store the handle of the bottom cell in each column (AddCell(4)) when you finish creating that column.
  2. After the loop, use one add_line to connect the negative port of column1’s bottom cell to the positive port of column2’s bottom cell.
open_system('Module_arc')
mdl = 'Module_arc';
bat_rec_model = find_system(mdl,'FindAll','on','Name','Module_arc');
% keep track of the bottom cell in each column:
bottomCellCol1 = [];
bottomCellCol2 = [];
%%% add Cell - basic CELL_unit:
% your code...................
switch i
case 2
Minus_2_Cell = find_system(mdl,'LookUnderMasks','All','FindAll','on','Name','NEG');
PH_minus2Cell=get(Minus_2_Cell,'PortHandles');
Neg_port= add_line(mdl,PH_minus2Cell.RConn,PH_AddCell{1}.LConn(1),'Autorouting','on');
bottomCellCol1 = AddCell(4); % "CELL 4"
case 1
Plus_2_Cell = find_system(mdl,'LookUnderMasks','All','FindAll','on','Name','POS');
PH_plus2Cell=get(Plus_2_Cell,'PortHandles');
Pos_port= add_line(mdl,PH_plus2Cell.RConn,PH_AddCell{1}.LConn(1), 'Autorouting','on');
bottomCellCol2 = AddCell(4); % "CELL 8"
end
end
% Connect NEG port of CELL 4 to POS port of CELL 8
PH_col1Cell4 = get(bottomCellCol1, 'PortHandles'); % bottom of column 1
PH_col2Cell4 = get(bottomCellCol2, 'PortHandles'); % bottom of column 2
add_line(mdl, PH_col1Cell4.LConn(2), PH_col2Cell4.LConn(1), 'Autorouting','on');

1 Comment

great Ted,
a very good approach.
It realy works...:-)
Now I need to figure out how to connet "go" and "from" tags for each column.
Thanks a lot

Sign in to comment.

Asked:

on 7 Mar 2025

Commented:

on 28 Mar 2025

Community Treasure Hunt

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

Start Hunting!