Normalize app designer window position

63 views (last 30 days)
Brandon Campanile
Brandon Campanile on 1 Dec 2018
Commented: Mike McCullough ongeveer 22 uur ago
I have an application I made in app designer that has the line app.UIFigure.WindowState = 'maximized'. The UIFigure has a default position of [1 1 1920 1080] and fits to my screen very nicely and maximizes very well. However, it's only working for my display; on anyone elses it does not scale correctly. The matlab application I am using is located here. The "normalized" property is not yet supported in app designer so I have to use the pixel units. Whenever I change the size of the UIFigure programmatically it does not resize the children (yes I have resize children on). I've tried the following:
d=get(0,'screensize');
app.UIFigure.Position = [1 1 d(3) d(2)];
However, this isn't changing the size of the children recursively. I even tried scaling it like this:
d = get(0,'screensize');
app.UIFigure.Position = [1 1 d(3)*app.UIFigure.Position(3)/1920 d(4)*app.UIFigure.Position(4)/1080];
The above works for some elements (not UIFigure), but I have too many to do that for each one. I also tried making a recursive function. I dont remember exactly, but it was something like this:
d = get(0,'screensize');
updatePosition(app.UIFigure)
function updatePosition(R)
if ~isempty(R.Children)
R.Children.Position = [1 1 d(3)*R.Children.Position(3)/1920 d(4)*R.Children.Position(4)/1080];
updatePosition(R.Children)
end
end
Nothing is working. App designer needs some serious TLC. Does anybody know of a way to get the app to size appropriately for different screen sizes and resolutions (short of a startup fnc that changes the position of every single element)?
  1 Comment
Chris Portal
Chris Portal on 3 Dec 2018
Do you have screenshots that shows what it looks like on your display vs someone elses, just to understand what you mean by “does not scale correctly”?

Sign in to comment.

Answers (5)

Jorge Barrasa Fano
Jorge Barrasa Fano on 6 Aug 2019
The line:
movegui(app.UIFigure,"center");
at the beginning of startupFcn works fine for me. No need to do pause() and it avoids playing with window coordinates.
  1 Comment
Andrew Diamond
Andrew Diamond on 27 Mar 2020
Edited: Andrew Diamond on 27 Mar 2020
This doesn't rescale the children for me. On a display smaller than the application size, It will move the app and seems to resize the figure to fit but not the children. I have whole gui elements that are simply not available because of this (they get chopped off in the now smaller figure). Resizing the window manually afterwards doesn't do anything to help. The FigureSizeChanged callback does not get called.
It does seem that if I stop in the debugger in the startufcn it will do it but I tried to emulate that with a pause(5) and a drawnow but that didn't work. I guess I could set up a timer to try and resize in a few seconds but it's pretty nasty

Sign in to comment.


Scott Guillaudeu
Scott Guillaudeu on 22 Jul 2019
In startupFcn, try pause(2) before setting app.UIFigure.Position. It's ugly and I don't know exactly why it works, but It seems to take a while to layout the figure the first time and it will only resize all the children after it's done the first layout.
This works for me on a Mac, but I haven't tried any other OSes.

Skander Ayoub
Skander Ayoub on 25 Nov 2021
Try this:
function startupFcn(app)
app.UIFigure.Visible = 'off';
movegui(app.UIFigure,"center")
app.UIFigure.Visible = 'on';
end
  1 Comment
Mike McCullough
Mike McCullough ongeveer 22 uur ago
Really appreciate this set of commands!
I searched google for 1 hour before finding this...

Sign in to comment.


Mekiedje Jöel
Mekiedje Jöel on 21 Jan 2019
Hello, i just have the same problem. Have you solved the problem ?

p su
p su on 2 Jan 2023
All elemetns in the position vector have to be resized (including the x and y).
app.UIFigure.Position = [10, 20, 200, 100];
d = get(0, 'screensize'); % [1, 1, 1920, 1200]
ratio = [d(3)/app.UIFigure.Position(3), d(4)/app.UIFigure.Position(4)];
app.UIFigure.WindowState = 'maximized';
app.Children.Position = app.Children.Position .* [ratio, ratio]; % [x, y, w, h] .* [ratio_w, ratio_h, ratio_w, ratio_h]
I do find out that the above resizing is not perfect, since the ratio was calculated based on the whole screen size, which will include the heights of task bar and title bar and miscalculate the ratio. In order to fixe it, use the maximized UIFigure position instead.
app.UIFigure = uifigure('Visible', 'on');
app.UIFigure.Position = [10, 20, 200, 100];
oldPos = app.UIFigure.Position;
app.UIFigure.WindowState = 'maximized';
drawnow; % Force to draw the uifigure first
newPos = get(app.UIFigure, 'Position'); % [1, 41, 1920, 1137]
ratio = [newPos(3)/oldPos(3), newPos(4)/oldPos(4)];
app.Children.Position = app.Children.Position .* [ratio, ratio]; % [x, y, w, h] .* [ratio_w, ratio_h, ratio_w, ratio_h]

Categories

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

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!