10 Lay Out a Programmatic UI
258 258 474 248
Other UI components, such as uicontrols, uitables, and uipanels have a Position
property, which you can use to set their location and size.
Units of Measure
The default units associated with the Position property depend on the component you are
placing. However, you can change the Units property to lay out your UI in the units of
your choice. There are six different units of measure to choose from: inches, centimeters,
normalized, points, pixels, and characters.
Always specify Units before Position for the most predictable results.
f = figure('Units','inches','Position',[4 3 6 5]);
Your choice of units can affect the appearance and resizing behavior of the UI:
- If you want the components inside the figure to scale proportionally with the figure
when the user resizes the window, set the Units property of the components inside the
figure to 'normalized'. - If you are developing a cross-platform UI, then you can set the Units property to
'points' or 'characters' to make the layout consistent across all platforms. - There might be other situations in which you want to lay out your UI using
nondefault units. For example, you might need to specify height and width values in
inches in order to conform to a specification. - When the value of the Units property is 'inches', 'centimeters', 'points',
'pixels', or 'characters', the component does not scale with the figure when
the user resizes the UI. To enable automatic scaling, set the Units property to
'normalized' after the code that specifies the Position in the other units.
Example of a Simple Layout
Here is the code for a simple UI containing an axes and a button. To see how it works,
copy and paste this code into the editor and run it.
function myui
% Add the UI components
hs = addcomponents;