10 Lay Out a Programmatic UI
'Units','pixels',...
'Tag','ax');
end
function plotsine(hObject,callbackdata)
theta = 0:pi/64:6*pi;
y = sin(theta);
plot(hs.ax,theta,y);
end
function resizeui(hObject,callbackdata)
% Get figure width and height
figwidth = hs.fig.Position(3);
figheight = hs.fig.Position(4);
% Set button position
bheight = 30;
bwidth = 70;
bbottomedge = figheight - bheight - 50;
bleftedge = 10;
hs.btn.Position = [bleftedge bbottomedge bwidth bheight];
% Set axes position
axheight = .75*figheight;
axbottomedge = max(0,figheight - axheight - 30);
axleftedge = bleftedge + bwidth + 30;
axwidth = max(0,figwidth - axleftedge - 50);
hs.ax.Position = [axleftedge axbottomedge axwidth axheight];
end
end
The resizeui function sets the location and size of the button and axes whenever the
user resizes the window:
- The button height, width, and left edge stay the same when the window resizes.
- The bottom edge of the button, bbottomedge, allows 50 pixels of space between the
top of the figure and the top of the button. - The value of the axes height, axheight, is 75% of the available height in the figure.
- The value of the axes bottom edge, axbottomedge, allows 30 pixels of space between
the top of the figure and the top of the axes. In this calculation, the max function
limits this value to nonnegative values.