Create panel container object
h = uipanel('PropertyName1',value1,'PropertyName2',value2,...)
h = uipanel(parent,'PropertyName1',value1,'PropertyName2',value2,...)
A uipanel groups components. It can contain user interface controls with which the end user interacts directly. It can also contain axes, other uipanels, and uibuttongroups. It cannot contain ActiveX® controls.
h = uipanel('PropertyName1',value1,'PropertyName2',value2,...) creates a uipanel container object in a figure, uipanel, or uibuttongroup. Use the Parent property to specify the parent figure, uipanel, or uibuttongroup. If you do not specify a parent, uipanel adds the panel to the current figure. If no figure exists, MATLAB® creates one. See the Uipanel Properties reference page for more information.
h = uipanel(parent,'PropertyName1',value1,'PropertyName2',value2,...) creates a uipanel in the object specified by the handle, parent. If you also specify a different value for the Parent property, the value of the Parent property takes precedence. parent must be a figure, uipanel, or uibuttongroup.
A uipanel object can have axes, uicontrol, uipanel, and uibuttongroup objects as children. For the children of a uipanel, the Position property is interpreted relative to the uipanel. If you move the panel, the children automatically move with it and maintain their positions relative to the panel.
After creating a uipanel object, you can set and query its property values using set and get.
Create a uipanel, a subpanel in the first panel, and a pushbutton in the subpanel.
Both panels use the default Units property value, normalized. (Whereas the default Units for the uicontrol push button is pixels.)
h = figure; hp = uipanel('Title','Main Panel','FontSize',12,... 'BackgroundColor','white',... 'Position',[.25 .1 .67 .67]); hsp = uipanel('Parent',hp,'Title','Subpanel','FontSize',12,... 'Position',[.4 .1 .5 .5]); hbsp = uicontrol('Parent',hsp,'String','Push here',... 'Position',[18 18 72 36]);
Create a uipanel that always matches the width of the figure, but maintains a constant height. The ResizeFcn keeps the uipanel whose Tag is StatusBar 20 pixels high, as wide as the figure, and attached to the top of the figure.
Create a file named match_width.m on your MATLAB path containing the code that follows. This code uses the Tag property to retrieve the uipanel handle, and the gcbo function to retrieve the figure handle. By default, the figure Units property is set to pixels. The code sets the panel Units to match, but restores them to their original setting before the function ends.
function match_width(hObject,eventdata) u = findobj(gcbo, 'Type','uipanel','Tag','StatusBar'); fig = gcbo; panelunits = get(u,'Units'); set(u,'Units','pixels'); % derive the new position for the panel figpos = get(fig,'Position'); upos = [1, figpos(4) - 20, figpos(3), 20]; set(u,'Position',upos); % restore units for the panel set(u,'Units',panelunits); end
Run this code in the Command Window to create a figure that specifies the match_width function as the ResizeFcn callback. (If you do not set the figure Visible property set to off initially, the uipanel does not display until the first time you resize the window. )
f = figure('Visible','off'); u = uipanel('Units','Pixels', 'Tag','StatusBar'); set(f,'ResizeFcn', @match_width); set(f,'Visible','on');
Resize the figure window to observe the effect of the ResizeFcn callback.
If you set the Visible property of a uipanel object to 'off', any child objects it contains (buttons, button groups, axes, etc.) become invisible along with the panel itself. However, doing this does not affect the settings of the Visible property of any of its child objects, even though all of them remain invisible until the uipanel's visibility is set to 'on'.