How can I connect 3 Simscape ports programmatically

6 views (last 30 days)
Hello everyone,
Recently I am having a simulink problem:
I would like to connect 3 Simscape Electrical Ports programmatically.
The result should look something like this:
For that I wrote a MATLAB Code that should create the above:
simulink
open_system(new_system('sys'));
% Add Blocks
add_block('nesl_utility/Connection Port','sys/1');
add_block('nesl_utility/Connection Port','sys/2');
add_block('ee_lib/Passive/Capacitor','sys/C');
% Move Blocks
set_param('sys/1','position',[0,0,30,30]);
set_param('sys/C','position',[0,100,30,130]);
set_param('sys/2','position',[0,200,30,230]);
% Find Position
par1 = get_param('sys/1','PortConnectivity');
parC = get_param('sys/C','PortConnectivity');
par2 = get_param('sys/2','PortConnectivity');
pos1 = par1.Position;
posC1 = parC(1).Position;
posC2 = parC(2).Position;
pos2 = par2.Position;
% Connect Blocks
add_line('sys', [pos1;posC1]);
add_line('sys', [pos2;posC2]);
add_line('sys', [pos1;pos2]);
The problem is, that one connection is always missing, and with the above code I get this:
Is there any solution for this?

Accepted Answer

Timo Dietz
Timo Dietz on 14 Jan 2022
Hello,
you can directly connect via the port pin names, instead of using the position information:
add_line('sys', '1/RConn1', 'C/LConn1')
add_line('sys', '2/RConn1', 'C/RConn1')
add_line('sys', '1/RConn1', '2/RConn1')

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!