| MATLAB® | ![]() |
| On this page… |
|---|
Use the align function to align user interface controls and axes. This function enables you to align the components vertically and horizontally. You can also distribute the components evenly, or specify a fixed distance between them.
A syntax for the align function is
align(HandleList,'HorizontalAlignment',...
'VerticalAlignment')where HorizontalAlignment can be None, Left, Center, Right, Distribute, or Fixed and VerticalAlignment can be None, Top, Middle, Bottom, Distribute, or Fixed. All handles in HandleList must have the same parent. See the align reference page for information about other syntaxes.
The following code creates three push buttons that are somewhat randomly placed. Each subsequent example starts with these same three push buttons and aligns them in different ways. Components are aligned with reference to their bounding box, shown as a blue dashed line in the figures.
b1 = uicontrol(fh,'Posit',[30 10 60 30],'String','Button 1'); b2 = uicontrol(fh,'Posit',[50 50 60 30],'String','Button 2'); b3 = uicontrol(fh,'Posit',[10 80 60 30],'String','Button 3');

Aligning Components Horizontally While Distributing Them Vertically
Aligning Components Vertically While Distributing Them Horizontally
The following statement moves the push buttons horizontally to the right of their bounding box. It does not alter their vertical positions. The figure shows the original bounding box.
align([b1 b2 b3],'Right','None');

The following statement moves the push buttons horizontally to the center of their bounding box and adjusts their vertical placement to create a fixed distance of 7 points between the boxes. The push buttons appear in the center of the original bounding box. The bottom push button remains at the bottom of the original bounding box.
align([b1 b2 b3],'Center','Fixed',7);

The following statement moves the push buttons vertically to the bottom of their bounding box and adjusts their horizontal placement to create a fixed distance of 5 points between the boxes. The push buttons appear at the bottom of the original bounding box.
align([b1 b2 b3],'Fixed',5,'Bottom');

![]() | Adding Components to the GUI | Setting Tab Order | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |