Programmatically connect SimScape blocks

5 views (last 30 days)
Jack Daniels
Jack Daniels on 4 Mar 2023
Answered: J Chen on 15 Mar 2023
I am connecting simscape blocks programatically.
add_block('fl_lib/Electrical/Electrical Elements/Resistor','mymodel/Res1');
set_param('mymodel/Res1','position',[140,80+40,180,120+40]);
add_block('fl_lib/Electrical/Electrical Elements/Capacitor','mymodel/Cap1');
set_param('mymodel/Cap1','position',[220,80+80,260,120+80]);
set_param('mymodel/Cap1','Orientation','down');
add_line('mymodel','Res1/Rconn 1','Cap1/Lconn 1');
add_block('fl_lib/Electrical/Electrical Elements/Electrical Reference','mymodel/Ref1');
set_param('mymodel/Ref1','position',[220,80+160,260,120+160]);
add_line('mymodel','Cap1/Rconn 1','Ref1/Lconn 1');
% set_param('mymodel/Ref1','Orientation','down');
add_block('fl_lib/Electrical/Electrical Elements/Resistor','mymodel/Res2');
set_param('mymodel/Res2','position',[220,120,180,160]);
add_line('mymodel','Res1/Rconn 1','Res2/Lconn 1');
by changing the value of the position of the Res2 I am having the issue:
set_param('mymodel/Res2','position',[220,120,180,160]);
> In new_model (line 12)
Error using new_model
Invalid setting in Resistor block (mask) 'Res2' for parameter 'Position'
Caused by:
Error using new_model
Invalid definition of rectangle. Width and height should be positive.
Can you advice what can be an issue?
How can I connect the Res2 to the Wire instead of connectting directly to the output of Res1?
How can I amke the graph wiring stright, aligned programatically?

Answers (1)

J Chen
J Chen on 15 Mar 2023
set_param('mymodel/Res2','position',[220,120,180,160])
is wrong. The third number (180) needs to be greater than the first number (220). The command needs to be something like
set_param('mymodel/Res2','position',[280,120,320,160]);
Adjust the parameters for the y-coordinate will made the wiring straight. For example
set_param('mymodel/Res1','position',[140,85+40,180,125+40]);
set_param('mymodel/Res2','position',[280,125,320,165]);

Categories

Find more on Electrical Sensors in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!