Lay Out a UI Programmatically
If your UI has another container, such as a uipanel or uibuttongroup, you can manage
the layout of the container’s child components in a separate callback function that you
assign to the SizeChangedFcn property.
The SizeChangedFcn callback executes only under these circumstances:
- The container becomes visible for the first time.
- The container is visible while its drawable area changes.
- The container becomes visible for the first time after its drawable area changes. This
situation occurs when the drawable area changes while the container is invisible and
becomes visible later.
Note:Typically, the drawable area changes at the same time the outer bounds change.
However, adding or removing menu bars or tool bars to a figure causes the outer bounds
to change while the drawable area remains constant. Therefore, the SizeChangedFcn
callback does not execute when you add or remove menu bars or tool bars.
This UI is a resizable version of the simple UI defined in “Example of a Simple Layout”
on page 10-34. This code includes a figure SizeChangedFcn callback called resizeui.
The resizeui function calculates new Position values for the button and axes when the
user resizes the window. The button appears to be stationary when the user resizes the
window. The axes scales with the figure.
function myui
% Add the UI components
hs = addcomponents;
% Make figure visible after adding components
hs.fig.Visible = 'on';
function hs = addcomponents
% Add components, save handles in a struct
hs.fig = figure('Visible','off',...
'Tag','fig',...
'SizeChangedFcn',@resizeui);
hs.btn = uicontrol(hs.fig,'String',...
'Plot Sine',...
'Callback',@plotsine,...
'Tag','button');
hs.ax = axes('Parent',hs.fig,...