add_line connection for to column cells
조회 수: 6 (최근 30일)
이전 댓글 표시
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
채택된 답변
Altaïr
2025년 3월 21일
편집: Altaïr
2025년 3월 21일
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
Altaïr
2025년 3월 28일
편집: Altaïr
2025년 3월 28일
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.
추가 답변 (1개)
TED MOSBY
2025년 3월 21일
편집: TED MOSBY
2025년 3월 21일
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:
- Store the handle of the bottom cell in each column (AddCell(4)) when you finish creating that column.
- 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');
참고 항목
카테고리
Help Center 및 File Exchange에서 Additional Math and Discrete에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


