File Exchange Pick of the Week

Our best user submissions

GUI Layout (Part 1)

This is a post from Jiro, one of our guest bloggers:

One of my favorite things to do in MATLAB is to build GUIs. This is Part 1 of 3 Pick of the Week entries on GUIs.

When I build GUIs, I think about not only the functionalities, but also the aesthetics of them. I like buttons and sliders to be a certain size, and I want them to resize appropriately when I change the size of the figure window.

In MATLAB GUIs, the handle graphics objects have a Units property which specifies the units to use for positioning the objects. Using the Normalized option allows the objects to resize proportionally with the figure. In order to keep some components from becoming oversized or undersized, I often define a ResizeFcn for the figure to adjust the positions of the objects. For example, take a look at the following example:

First, here's the GUI at a particular size:

Next, here's the same GUI after resizing it to a smaller size:

Notice how the size of the push button remained the same and the slider bar and the axes adjusted their size appropriately to fill the remaining space.


 function myResizeFcn(varargin)
   % Figure resize callback
   %   Adjust object positions so that they maintain appropriate
   %   proportions
   fP = get(figH, 'Position');
   set(sliderH, 'Position', [10       , 10, fP(3)-130, 25       ]);
   set(buttonH, 'Position', [fP(3)-110, 10, 100      , 25       ]);
   set(axesH  , 'Position', [50       , 85, fP(3)-100, fP(4)-130]);

Every time the figure is resized, it determines the current figure size and then positions the objects accordingly.

This has worked nicely for me, but I always find things out in the File Exchange bucket that would make my life easier. Stay tuned for Part 2 and Part 3, in which I will introduce a couple of entries that I found useful for GUI layouts.


Tell us about some of the things you do to make your GUI nicer (both functionally and aesthetically).

Written by Jiro Doke, Senior Applications Engineer, The MathWorks, Inc.

Published with MATLAB® 7.5



To leave a comment, please click here to sign in to your MathWorks Account or create a new one.