Clear Filters
Clear Filters

GUI full screen with centred figure?

7 views (last 30 days)
Nils on 31 Mar 2014
Answered: Josep Llobet on 14 Jul 2022
let's say I have a 500 x 500 pixels GUI. When I set the resize funktion in the property inspector "on" and maximze the window my GUI will move to the left bottom and the rest of the screen is filled with the grey background colour.
Is it possible to have my GUI centred in the middle of the screen when I maximize the GUI?
Thanks in advance Nils
  1 Comment
Adam on 17 Oct 2014
Do you want all the components to stay the same size or resize themselves to fill the same relative space that they did in the original size?
I tend to set all my GUI Components to have 'normalized' units so that when I resize the window they stay proportionally as they were. It isn't ideal, certainly for down-sizing, but it does avoid having all the GUI components just plonked in one corner.

Sign in to comment.

Answers (3)

Jakob Sørensen
Jakob Sørensen on 31 Mar 2014
It is definitely possible to get the figure size using get(gcf, 'position'), and then calculate where the figure position should be, to be centered (as this depends only on figure size and axis size). There might be a simpler way, but not one I know.

Image Analyst
Image Analyst on 17 Oct 2014
Edited: Image Analyst on 17 Oct 2014
I found some of my programs broke with R2014b because I was using one of Yair's undocumented calls to maximize the GUI window:
% Function to maximize the window via undocumented Java call.
% Reference:
FigurejFrame = get(handle(gcf),'JavaFrame');
Unfortunately it barfs up a bunch of red java messages with R2014b.
Error using test (line 60)
Java exception occurred:
at com.mathworks.hg.peer.FigureFrameProxy.setMaximized(
at com.mathworks.hg.peer.FigureMediator.setMaximized(
Is there an alternate method that you know about, other than using set(gcf, 'Position'....) which doesn't really maximize.
Even Yair's suggest fix for R2014b (HG2) does not work:
% Enlarge figure to full screen.
jFrame = get(handle(gcf),'JavaFrame');
% set(gcf,'Resize','off');
jFrame.fHG1Client.setMaximized(true); % HG1
jFrame.fHG2Client.setMaximized(true); % HG2
UNLESS there's a breakpoint set anywhere in the try/catch - then it works fine. Bizarre.
  1 Comment
Adam on 17 Oct 2014
Edited: Adam on 17 Oct 2014
I've also had problems with using the undocumented functionality after the R2014b upgrade, but not to the extent I could really pin down the cause. Just seemed to be a general case of figures no longer being in focus when they were in R2014a for e.g. findjobj to work correctly or that functionality that used to work in the OpeningFcn of a GUI that now doesn't work presumably because the GUI is not visible yet and R2014b doesn't like that whereas R2014a was fine with it.
I also had functionality in a Resize function that was triggering before the GUI became visible that stopped working similarly.
Unfortunately I ran out of time on getting to the bottom of these so had to settle for inferior documented solutions or just worse UIs in some cases.

Sign in to comment.

Josep Llobet
Josep Llobet on 14 Jul 2022
For obtain a centered GUI figure based on the screensize, in MATLAB App, you can try it:
% Obtain GUI position
UIFigure_position = app.UIFigure.Position; %100 100 602 389
% inici final amplada llargada
%Obtain screen position
screensize_position = get(0, 'ScreenSize'); % 1 1 1920 1080
% Construct new positions based on the screen and figure positions.
amplada_new = ceil(screensize_position(3)/2 - UIFigure_position(3)/2);
llargada_new = ceil(screensize_position(4)/2 - UIFigure_position(4)/2);
UIFigure_position_new = [amplada_new, llargada_new, UIFigure_position(3), UIFigure_position(4)];
% Add this position to the MATLAB App figure:
app.UIFigure.Position = UIFigure_position_new;
I am going to put it into a function in MATLAB File exchange
I hope will be useful


Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!