This is Part 2 of 3 Pick of the Week entries on GUIs, written by guest blogger Jiro
In Part 1, I talked about how I use the ResizeFcn property of the figure to adjust the layout of my UI controls. Well, I'm always looking for easier ways to do layouts.
Jerome Briot wrote these utility tools SETPOS and GETPOS that give you more flexibility in defining the positions of objects. With these, you can provide individual units for the four numbers that define the Position property:
% Use "normalized" for X and Width. % Use "pixels" for Y and Height. setpos(h,'0.25nz 100px 0.5nz 300px');
There are other nice features, such as incrementing by certain values:
% Increase the width by 200 pixels setpos(h,'# # +200px #');
Or positioning objects relative to other objects:
setpos(figure, '# # 400px 300px'); u(1) = uicontrol('string', '(0,0) gcf'); setpos(u(1), '0 0 60px 40px') u(2) = uicontrol('string', '(0,0) gca'); setpos(u(2), '0 0 60px 40px', gca)
This function will allow me to do precise positioning of UIs with fewer lines of code. Thanks Jerome! Next week, I will introduce a different tool that assists in the overal GUI design. Stay tuned for Part 3...
Comments?
Tell us what you think about when you are laying out your GUI components. Is it readability? Or is it the level of informational content?
Written by Jiro Doke, Senior Applications Engineer, The MathWorks, Inc.
Get
the MATLAB code
Published with MATLAB® 7.5



Doug’s guest blogger, Jiro, is one seriously dedicated MathWorker. This guy is so amazing, he can even write MATLAB while driving his car, merging onto Route 9 while exiting The MathWorks, on Apple Hill Drive. Truly impressive.
Take a close look, and zoom in real close; note the one L-shaped membrane sitting at the on-ramp on Route 9:
http://www.mathworks.com/matlabcentral/reports/fileexchange/contributormap/
All of the other mere mortal MathWorkers are hard at work in their respective offices.
;-)
Tim,
I’m glad to hear that you find my driving skills (while writing MATLAB) impressive. Er, or was it the other way around…?
Anyway, back to MATLAB!
Hey there!
I have an issue, which seems to have relevance to your article.I have this Matlab GUI and I need it to be independent of screen resolution. What I mean by this is that I want it to resize itself according to whatever the screen resolution is. Currently, none of the objects within the window seem to resize/relocate accordingly. So what I have is text boxes that cut off text if the screen resolution is changed :(.
Thanks in advance,
Nikolay
Nikolay,
Perhaps you can use “normalized” units for your fonts (FontUnits). Then when the controls are resized, the text inside it will also be proportionally resized.
If you want the size to be independent of screen resolution, you can set it Units to be absolute units, i.e. not normalized. Then the text should not be clipped.