ashkan
ashkan

Reputation: 476

How do you make a MATLAB's `uifigure` appear in the center of the screen?

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.

Upvotes: 0

Views: 2330

Answers (2)

Ray
Ray

Reputation: 11

Use

movegui(app.UIFigure,'center')

Upvotes: 1

user69221
user69221

Reputation: 196

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])

Upvotes: 3

Related Questions