0

One can easily use the Position property to place a uifigure in the specified location of the screen. E.g., fig = uifigure('Position',[1,1,300,300]);. Is there any way to place it immediately on the center of screen.

There is a movegui command which is helpful for this task. However, it does this work in two steps (first, displays the figure, then moves it). This results in a not smooth experience for the user.

ashkan
  • 476
  • 6
  • 14

2 Answers2

3

We need to get the screen size to determine the center. The code below will create a figure at the center of the screen.

% width and height of the figure
width = 300;
height = 300;

% screen size
sz = get( 0, 'ScreenSize');

% center position
x = mean( sz( [1, 3]));
y = mean( sz( [2, 4]));

fig = uifigure( 'Position', [x - width/2, y - height/2, width, height])
user69221
  • 176
  • 6
1

Use

movegui(app.UIFigure,'center')
Ray
  • 11
  • 1
  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney May 20 '22 at 00:12