how to put an app designer window in the center of the screen?

134 views (last 30 days)
hi, i've been designed an app in matlab, now the problem is that i want to put the app window in the center of the screen without setting position by myself. i want to write some codes so that everytime anyone open the app, the window is automatically always in the center. is it possible and how to realize?
thank you!
the window now:
what i expect:
without setting this by the app user:

Accepted Answer

dpb
dpb on 23 Jun 2022
Edited: dpb on 23 Jun 2022
While you can do this using the .ScreenSize and .MonitorPositions properties retrieved from groot, I'd say it's quite rude and very egotistical to make your app appear in the middle of your users' screens regardless of their preferences.
It would be much better to let it default originally(*) and then save the position on close in a usersettings data structure and reload in last position on subsequent opens. Each user gets/keeps his own screen space that way.
(*) Or, alternatively, add a user preferences item in the menu that lets user select a given general location -- 'NW', 'NE', etc., ... something like what legend has for options. Just don't be adamant that your app must own the middle of the screen.
  3 Comments
Adee
Adee on 6 Nov 2022
I assume the need to set the initial location to somewhere specific comes from the fact that the initial location in appdesigner is hard-coded in pixel units (in createComponents), and depending on the window size, it may not be suitable for all users.
The programmer may have a huge display and set the initial coordinates accordingly. The code generated by appdesigner does not check for the display size when the app is executed (a feature to look forward to?), and if a user has a smaller display, the window may be partly or completely off the display. Moving such windows is possible, but quite painful, and many users don't know how. It would not be rude or egoistical to help these users.
The initial location can be changed in the window properties in appdesigner, but it seems that appdesigner does not enable changing the units to normalized. Another feature to look forward to?

Sign in to comment.

More Answers (2)

Adee
Adee on 6 Nov 2022
Edited: Adee on 6 Nov 2022
The built-in function movegui can be used.
movegui(fig,'center')
Will move the figure fig to the center of the current display. Other locations are also possible - see "doc movegui".
It works with appdesigner figures too.
You can call this function in a startupfcn callback. For example, if your GUI figure is called main_UIFigure:
function startupFcn(app)
movegui(app.main_UIFigure, 'center');
end

Image Analyst
Image Analyst on 27 Jun 2022
I have a function to center the GUI on the screen. See below.
% Centers the figure on the screen.
function CenterFigure(handles)
try
% The figure Position property does not include the window borders,
% so this example uses a width of 5 pixels on the sides and bottom and 30 pixels on the top.
% borderWidth = 5;
% titleBarWidth = 30;
% Ensure root units are pixels:
g = groot;
g.Units = 'pixels';
% Get the screen size in pixels:
screenSize = g.ScreenSize;
screenWidth = screenSize(3);
screenHeight = screenSize(4);
% Get the size of the window.
childrenFigure = g.Children;
if numel(childrenFigure) > 1
return;
end
childrenFigure.Units = 'pixels';
initialFigurePosition = childrenFigure.Position;
% Create an array that will center it.
centeredX = (screenWidth - initialFigurePosition(3)) / 2;
centeredY = (screenHeight - initialFigurePosition(4)) / 2;
centeredPosition = [centeredX,...
centeredY,...
initialFigurePosition(3),...
initialFigurePosition(4)];
% Send the centered coordinates to the figure to actually cause the figure to move.
childrenFigure.Position = centeredPosition;
catch ME
errorMessage = sprintf('Error in program %s, function %s(), at line %d.\n\nError Message:\n%s', ...
mfilename, ME.stack(1).name, ME.stack(1).line, ME.message);
uiwait(errordlg(errorMessage));
return;
end
return; % from CenterFigure()

Categories

Find more on Migrate GUIDE Apps 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!

Translated by