File Exchange Pick of the Week

February 22nd, 2008

GUI Layout (Part 2)

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

4 Responses to “GUI Layout (Part 2)”

  1. Tim Davis replied on :

    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.

    ;-)

  2. Jiro replied on :

    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!

  3. NikolayS replied on :

    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

  4. jiro replied on :

    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.

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Bob, Brett & Jiro share their favorite user-contributed submissions from the File Exchange.

  • Zach: Hi Doug and Les, I didn’t have a lot of time to mess with this, but I did find a work-around. I plotted...
  • hamed: k
  • Les: @Zach This isn’t exactly what you are looking for but at least it puts all three parameters on the same...
  • Zach: Thanks for your suggestions Doug. I’ll give that a shot and see what happens. I’ve seen many of...
  • Doug: @Zach, I would say to use plotYYY, because that is close to what you want, but using depth as Y makes sense....
  • Doug: @Teja, I think this will work: http://www.mathworks .com/access/helpdesk /help/techdoc/ref...
  • Gify: merry christmas :) nice christmas tree! Regards, Janet Gify
  • Teja: Dear Doug Is there anyway to plot a surface from nonuniform data without meshgrid and griddata? Basically i...
  • Zach: I’m working with geophysical data, so I’d like to produce a depth profile. The y-axis would be...
  • Doug: @Ashok First, please do not use variable names that are MATLAB commands (std and mean). Second, p(j) should be...

These postings are the author's and don't necessarily represent the opinions of The MathWorks.