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

Technical Source
2 min readOct 20, 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,

© 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?

NOTE:-

Matlabsolutions.com provide latest MatLab Homework Help,MatLab Assignment Help for students, engineers and researchers in Multiple Branches like ECE, EEE, CSE, Mechanical, Civil with 100% output.Matlab Code for B.E, B.Tech,M.E,M.Tech, Ph.D. Scholars with 100% privacy guaranteed. Get MATLAB projects with source code for your learning and research.

Expert Answer

John Michell answered . 2021–10–20 09:58:53

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');

SEE COMPLETE ANSWER CLICK THE LINK

--

--

Technical Source

Simple! That is me, a simple person. I am passionate about knowledge and reading. That’s why I have decided to write and share a bit of my life and thoughts to.