how to put an app designer window in the center of the screen?
Ältere Kommentare anzeigen
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:
2 Kommentare
Ricardo Carvalho
am 4 Apr. 2023
What solution did you find for this problem?, the answers contained in the other comments do not work
dpb
am 4 Apr. 2023
What, specifically did you try? I've seen no problem in using <movegui> to one of the preset positions when I've tried; as noted, however, all the apps I've built seem to work just fine with the idea espoused in the Answer I gave, let the very first use of the app open at the default location, then save and restore the position as the user left it the previous time when returns. Each user has their own set of usersettings if the app is shared, depending upon the user login name.
Show us an example trial app startup code that "doesn't work" and then define what that means, specifically.
Akzeptierte Antwort
Weitere Antworten (2)
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
am 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()
Kategorien
Mehr zu Startup and Shutdown finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!