MATLAB: Problems with a resize function callback

callbackMATLABresizefcn

Hi,
I'm developing a programmed GUI (using the nice GUI toolbox layout available on file exchange). What I want to do is to define minimal length and minimal height of the GUI figure to automatically resize it to avoid displaying problems when the user resize the screen.
To do so, I defined the following resize function callback associated to the figure GUI (I use nested functions, so it is not a variable problem)
function myGUI
mainFig=figure('Position',[139 122 1204 675]);
set(mainFig,'ResizeFcn',@Resize_clbk);
min_length=1200;
min_heigth=600;
%[.....]
function Resize_clbk(hObject,Event)
actual_pos=get(hObject,'Position');
if actual_pos(3)<min_length
set(hObject,'Pos',[actual_pos(1:2) min_length actual_pos(4)]);
end
if actual_pos(4)<min_heigth
set(hObject,'Pos',[actual_pos(1:3) min_heigth]);
end
end
end
This function works nice when I resize the figure from both rigth and bottom borders of the window, but it doesn't works any time when I resize the window from the left or top border, or any corner.
Does anyone have a solution ?
Thank's in advance for your help ^^

Best Answer

This problem is not related to the GUI layout toolbox, but to the resizing of all Matlab figures. I've tried a lot of different tricks without success. Only on the Java level defining a minimum size is reliable:
FigH = gcf; % [EDITED: This works under R2009a, but fails for 2011b]
drawnow;
jFrame = get(handle(FigH), 'JavaFrame');
jProx = jFrame.fFigureClient.getWindow();
jProx.setMinimumSize(java.awt.Dimension(200,200));
The drawnow is required in R2009a, otherwise the limitation feature is active.
A nicer solution is to manage the resizing manually: Set 'Resize' to 'off', a user-defined resize function is triggered by the WindowsButtonDownFcn, when the cursor is less than 5 pixels from the window border. Check, if the mouse is on the left or right side of the figure. If the left side has been selected, the mouse motion is applied to the origin of the figure only until the distance to the right side is above the wanted limit.
Drawbacks: 1. The cursor is only caught inside the inner position of the figure. 2. The implementation is neither trivial nor lean. I assume you want to use the GUI layout toolbox to avoid writing a lot of special codes for standard tasks.
[EDITED] This works for 2009a and R2011b (I cannot test it under R2012b):
FigH = gcf; drawnow;
jFrame = get(handle(FigH), 'JavaFrame');
jWindow = jFrame.fHG1Client.getWindow;
jWindow.setMinimumSize(java.awt.Dimension(200,200));
  • R2011b: The figure size is not reduced below the minimum size during mouse movement.
  • R2009a: During resizing the minimum size is not considered. After the mouse is released, the window width and height is set accordingly. This moves the right window side, when you drag the left side below the limit.
Related Question