MATLAB Creating Graphical User Interfaces

(Barry) #1
UI That Uses Persistent Data

% When docked, get out
return
end
% If resized figure is smaller than original figure, then compensate.
% However, do not change figure size if it is docked; just adjust
% uicontrols
if ~strcmp(get(hObject,'WindowStyle'),'docked')
if Figure_Size(3) < Original_Size(3)
% If the width is too small then reset to origianl width
set(hObject,'Position',[Figure_Size(1) ...
Figure_Size(2) ...
Original_Size(3) ...
Original_Size(4)])
Figure_Size = get(hObject,'Position');
end


if abs(Figure_Size(4) - Original_Size(4)) > 10 % pixels
% Do not allow the height to change
set(hObject,'Position',[Figure_Size(1) ...
Figure_Size(2)+Figure_Size(4)-Original_Size(4) ...
Figure_Size(3) ...
Original_Size(4)])
end
movegui(hObject, 'onscreen')
end


% Get Contact_Name field Position for readjusting its width
C_N_pos = get(handles.Contact_Name,'Position');
ratio = Figure_Size(3) / Original_Size(3);
% Reset it so that its width remains proportional to figure width
% The original width of the Contact_Name box is 72 (characters)
set(handles.Contact_Name,'Position',[C_N_pos(1) ...
C_N_pos(2) ...
ratio * 72 ...
C_N_pos(4)])


Keeping Resized Figure On Screen


The SizeChangedFcn callback calls movegui to ensure that the resized UI is on screen
regardless of where you release the mouse.


The first time it runs, the UI displays at the size and location specified by the figure
Position property. This property was set with the Property Inspector when the UI was
created and it can be changed it in GUIDE at any time.

Free download pdf