Ask an expert. Trust the answer.

Your academic and career questions answered by verified experts

MATLAB: Automatic resizing of GUI components/fonts

Date: 2022-07-27 10:41:39

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. 

Expert Answer:

s: 

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! 

 

Why Matlabhelpers ?

Our Matlab assignment helpers for online MATLAB assignment help service take utmost care of your assignments by keeping the codes simple yet of high-quality. We offer the most reliable MATLAB solutions to students pursuing their Computer Science course from the Monash University, the University of Sydney, the University of New South Wales, the University of Melbourne; to name a few. Approach us today for best Matlab solutions online!

Our Comprehensive Matlab Assignment Help Services

Personalized Tutoring:Our team of MATLAB experts offers one-on-one tutoring sessions tailored to your specific needs. Whether you’re struggling with basic concepts or advanced algorithms, we provide clear, step-by-step guidance to help you understand and master MATLAB.

Assignment Assistance:Facing tight deadlines or complex assignments? We’re here to help! From initial problem analysis to code development and debugging, we offer full-spectrum support to ensure your assignments meet the highest standards.

Project Development: Need help with a research project? Our specialists can assist you in designing and implementing robust MATLAB solutions. We cover everything from project planning and data collection to coding, simulation, and result analysis.

Coursework Support: We provide comprehensive support for your coursework, helping you understand lectures, complete lab exercises, and prepare for exams. Our goal is to ensure you grasp the core principles and practical applications of MATLAB.

Thesis and Dissertation Guidance:Writing a thesis or dissertation? Our experts can assist you in incorporating MATLAB for data analysis, modeling, and simulation. We help you develop a strong methodological framework and ensure your research stands out.

whatsApp order on matlabhelpers.com

telegram order on matlabsolutions.com