Monday, November 6, 2017

[Matlab GUI] Resize the components such as axes, pushbutton, and so on on figure

There are two ways to resize components in figures when developing GUIs with GUIDE as below.

1. Menu Tools>GUI options>Resize behavior : Proportional
2. Menu Tools>GUI options>Resize behavior : Other (Use SizeChangedFcn)

Let's figure out two ways in details.
1. Menu Tools>GUI options>Resize behavior : Proportional
It is easy and simple to resize the components in figure with only selecting this option. However, the size is always changed proportional to the size of figure.
The units of all components are changed to 'normalized'.
Therefore, if you set the units of some components to other units which are not 'normalized', then the resize of those components is unavailable.

2. Menu Tools>GUI options>Resize behavior : Other (Use SizeChangedFcn)
If you select the resize behavior 'Other', then you have to create the SizeChangeFcn.

This function is easily added to the code by right-clicking on the GUIDE layout.

Example code
1. GUIDE layout
2. OpeningFcn function
% --- Executes just before resizeTest is made visible.
function resizeTest_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to resizeTest (see VARARGIN)

% Choose default command line output for resizeTest
handles.output = hObject;

% Change the units of components
set(handles.pushbutton1,'Units','characters');
set(handles.uipanel1,'Units','characters');
set(handles.axes1,'Units','characters');
set(handles.figure1,'Units','characters');
% Get the initial position
fp = get(handles.figure1,'Position');
bp = get(handles.pushbutton1,'Position');
pp = get(handles.uipanel1,'Position');
ap = get(handles.axes1,'Position');

handles.fp = fp;
handles.bp = bp;
handles.pp = pp;
handles.ap= ap;
handles.yfromup = fp(4) - bp(2);assignin('base','yf',handles.yfromup);
% Update handles structure
guidata(hObject, handles);

3. resize function added by right-clicking

% --- Executes when figure1 is resized.
function figure1_SizeChangedFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

set(handles.pushbutton1,'Units','characters');
set(handles.uipanel1,'Units','characters');
set(handles.axes1,'Units','characters');
set(handles.figure1,'Units','characters');

figpos = get(handles.figure1,'Position');
buttonpos = get(handles.pushbutton1,'Position');
panelpos = get(handles.uipanel1,'Position');
axespos = get(handles.axes1,'Position');

% Set a push button position to fix the size and position
btopgab = handles.fp(4) - handles.bp(2);
buttonbottom = figpos(4) - btopgab;
set(handles.pushbutton1,'Position',[buttonpos(1) buttonbottom buttonpos(3) buttonpos(4)]);
% Set a uipanel position to fix the size and position
ptopgab = handles.fp(4) - handles.pp(2);
panelbottom = figpos(4) - ptopgab;
set(handles.uipanel1,'Position',[handles.pp(1) panelbottom panelpos(3) panelpos(4)]);
% Set an axes position
axheight = figpos(4) * 0.7;
atopgab = handles.fp(4) - (handles.ap(2) + handles.ap(4));
axbottom = figpos(4) - atopgab - axheight;
axwidth = figpos(3) - handles.ap(1) - figpos(3)*0.05;
set(handles.axes1,'Position',[handles.ap(1) axbottom axwidth axheight]);

guidata(hObject,handles);


4. Implementation
1) Before
















2) After resize















Download

No comments:

Post a Comment