How to set the nearest position of the block from the Simulink from Matlab script?

16 views (last 30 days)
Hi!
I am working on automated Simulink models transformations (creating models from Matlab Scipt) and I need to:
(a) add a block to simulink from the Matlab script - DONE,
(b) connect it to desired port - DONE,
(c) put this block close to the connected port - NEED HELP.
I have found that the set_param function is able to change the position, but I have to know the exact coordinations:
set_param(['sampleModel' '/' 'From'],'Position',[0 50 0 50])
I need to set the nearest possible location of the block to its connected port. It is something like:
set_param(['sampleModel' '/' 'From'],'Position','Nearest'). There is any option in 'Position' that there is no need to put exact coordinations but only "Nearest"?
How can I achieve that?
Thank you in advance for help! Below I put the graphical explanation of the question:

Accepted Answer

C B
C B on 7 Oct 2021
Edited: C B on 7 Oct 2021
@Ewelina Cholodowicz I hope this will help you
For doing this you need to extract position of ports (both inport and outport) of each subsystem then.
you can do like below
Modelname = 'dummy';
new_system(Modelname);
open_system(Modelname);
add_block('built-in/Subsystem',[Modelname '/Subsystem'],'Position',[200 200 300 500]);
add_block('simulink/Sinks/Out1',[Modelname '/Subsystem/out1']);
add_block('simulink/Sources/In1',[Modelname '/Subsystem/in1']);
%% from here your point C begins
%get porthandles
PH = get_param([Modelname '/Subsystem'],'PortHandles');
%get position of inport and outport ports of subsystem
ipportpos = get(PH.Inport,'Position');
opportpos = get(PH.Outport,'Position');
%add from and goto blocks
gotoblockhandle = add_block('simulink/Signal Routing/From',[Modelname '/goto']);
fromblockhandle = add_block('simulink/Signal Routing/Goto',[Modelname '/from']);
%set position near to subsystem you can change 70 100 and 7 on how far you
%want to keep and size of block
set_param(fromblockhandle,'Position',[opportpos(1)+70 opportpos(2)-7 opportpos(1)+100 opportpos(2)+7 ]);
set_param(gotoblockhandle,'Position',[ipportpos(1)-100 ipportpos(2)-7 ipportpos(1)-70 ipportpos(2)+7 ]);
  3 Comments
Ewela
Ewela on 11 Oct 2021
@Chetan Bhavsar How you calculate the values: 70, 100, 7?
%set position near to subsystem you can change 70 100 and 7 on how far yo
C B
C B on 11 Oct 2021
Edited: C B on 11 Oct 2021
as we have two co-ordinates x and y in port position
opportpos(1) is x (Horizontal)
opportpos(2) is y (vertical)
Here 70 is Offset(change to take it far or near) and actual width of block is 30 (change for changing size of block)
[opportpos(1)+70 opportpos(2)-7 opportpos(1)+70+30 opportpos(2)+7 ]
as y will remain same we just need to set height of block so we need equal hight above and below so we will do -7 and +7 (height of block 14)
in above figure you can also see 360-330 = 30 (width of block 30)
so you have to add 30 to offset.This was for inport and outport
for from and goto of below size you can add 40 as offset and -14 +14
width=40 Height=28
do ask again if any point is not clear.

Sign in to comment.

More Answers (0)

Categories

Find more on Programmatic Model Editing in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!