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

17 views (last 30 days)
Ewela on 7 Oct 2021
Edited: Chetan Bhavsar on 11 Oct 2021
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:

Chetan Bhavsar on 7 Oct 2021
Edited: Chetan Bhavsar on 7 Oct 2021
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);
%% 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');
%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 ]);
Chetan Bhavsar 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.

R2019b

### Community Treasure Hunt

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

Start Hunting!

Translated by