I am having problems in trying to make my MATLAB GUIs automatically resizeable. After exhaustively searching the web for help and lots of testing, I could not find out a solution.
I have been developing a simple GUI (with MATLAB, without using GUIDE) in my laptop (Screen size/resolution = 1366x768). A very simplified version looks like this:
When I run the same GUI in my desktop computer (Screen size/resolution = 1920x1080), it shows in the following way:
The dimensions of the GUI are automatically initialized taking into account the screensize (the code is provided in the bottom of this post). As you can see (highlighted by the red arrows), the fonts/spacing between components do not automatically resize so that the GUI has the same aspect no matter where we run the file.
Additionally, when the GUI is manually resized, some overlap of the components occurs:
The code used for this minimal working example is the following:
function resizingGUIexample()
%% SET UP GUI
hdl.mainfig = figure();
% MANAGE FIGURE DIMENSIONS -------------------------------------------------------------------------------------
set(hdl.mainfig, 'Units', 'pixels');
dims = get(0, 'ScreenSize');
screenHeight = dims(4);
verticalMargins = floor((0.2*screenHeight)/2); % =10% of the screen height in each side
figureHeight = (0.8*screenHeight);
figureWidth = (0.8*screenHeight)*(4/3); % 4/3 Aspect Ratio
set(hdl.mainfig, 'Position', [0, verticalMargins, ...
figureWidth, figureHeight]);
movegui(hdl.mainfig,'center') % move GUI to center
color = get(hdl.mainfig,'Color'); % get background color to hide static texts, etc...
% AXES ---------------------------------------------------------------------------------------------------------
hdl.axes = axes('Parent', hdl.mainfig, ...
'Units', 'Normalized', ...
'Position', [0.295 0.05 0.63 0.63*(4/3)]);
% PUSH BUTTONS -------------------------------------------------------------------------------------------------
hdl.donePB = uicontrol(hdl.mainfig, ...
'Position', [0.85 0.91 0.075 0.075], ...
'String', 'Done', ...
'Fontsize', 16, ...
'Units', 'normalized', ...
'FontWeight', 'Bold');
% BUTTON GROUP and RADIO BUTTONS -------------------------------------------------------------------------------
hdl.buttonGroup = uibuttongroup('Parent', hdl.mainfig, ...
'FontSize', 16, ...
'FontWeight', 'Bold', ...
'BackgroundColor', color, ...
'Units', 'Normalized', ...
'Position', [0.05 0.69 0.2 0.2]);
titleBG = sprintf('Intensity\nNormalization');
set(hdl.buttonGroup, 'Title', titleBG);
hdl.VolumeRB = uicontrol(hdl.buttonGroup, ...
'Style', 'radiobutton', ...
'String', 'Volume', ...
'FontSize', 14, ...
'FontWeight', 'Bold', ...
'Units', 'normalized', ...
'BackgroundColor', color, ...
'Position', [0.1 0.67 0.8 0.3]);
hdl.SliceRB = uicontrol(hdl.buttonGroup, ...
'Style', 'radiobutton', ...
'String', 'Slice', ...
'FontSize', 14, ...
'FontWeight', 'Bold', ...
'Units', 'normalized', ...
'BackgroundColor', color, ...
'Position', [0.1 .25 0.8 0.3]);
end
Any ideas of how I can solve these issues?
Thanks a lot in advance.
Kind regards,
Fábio Nery
EDIT1: I am also extremely open to suggestions for better ways to initialize the GUI dimensions and strategies for avoiding problems when running GUIs in different monitors/screen resolutions.
Firstly, well done for not using GUIDE - you have passed the first test :)
I strongly recommend that you take a look at, and use, Ben Tordoff's GUI Layout Toolbox. Although you can do this sort of thing using the ResizeFcn
property, I can tell you that it's far easier with GUI Layout Toolbox, which just takes care of that stuff for you.
Managing a GUI that may be run on different (maybe multiple) monitors at different sizes and resolutions is a pain. I'd recommend specifying up front a range of sizes/resolutions that you're going to support, and sticking to that (even erroring if the application finds itself on an unsupported setup), rather than trying to be fully general. If you have to make everything work even on a lowest common denominator setup, you may have to sacrifice ease of us on a more normal setup.
You seem to have discovered get(0, 'ScreenSize')
and the movegui
command. Other useful things which come to mind are get(0, 'MonitorPositions')
, get(0, 'ScreenPixelsPerInch')
, and using the OuterPosition
rather than Position
property of figures.
Hope that helps!