Matlab using in sSimulink

6 views (last 30 days)
Tommy
Tommy on 6 Mar 2025
Answered: Altaïr on 20 Mar 2025
Hi,
I want to connect PCM_port to pos CELL input model created by simple code (file.m)
I can not understand why it does not works well.
results in attached pictures
Required result of pic - connection required.
Thank for support.
the following code:
pen_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:2 %% loop for 13 cells per column
nl=num2str(v + 2*(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
end
%%% connect the positive 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}.LConn(1,1), 'Autorouting','on');

Answers (1)

Altaïr
Altaïr on 20 Mar 2025
Hey @Tommy,
In the shared code, the line PH_AddCell{v}=get(AddCell(v),'PortHandles'); only stores port handles for blocks in the current column, which gets overridden with each iteration as i changes. To maintain a matrix of handles, use:
PH_AddCell{v,i}=get(AddCell(v),'PortHandles');
Assumptions for the blocks CELL_Unit/CELL 1 and CELL_Unit2/CELL 1 are as follows:
  • In CELL_Unit/CELL 1, port 1 is positive (pos) and port 2 is negative (neg).
  • In CELL_Unit2/CELL 1, port 1 is negative (neg) and port 2 is positive (pos).
  • Port locations are set to "Left" for both pos and neg in these blocks.
The the two additional connections to the cell array, can finally be indroduced as shown below:
% connect the positive 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');
add_line(mdl,PH_plus2Cell.RConn,PH_AddCell{1,1}.LConn(1,1), 'Autorouting','on');
% connect neg and pos of the two cells in last row
add_line(mdl,PH_AddCell{numRows,1}.LConn(1,2),PH_AddCell{numRows,2}.LConn(1,2), 'Autorouting','on');
Here's the final result:
For a quick start guide on programmatically modeling, visit:

Categories

Find more on Simulink Functions 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!