MATLAB Answers

Is it possible to maximize, minimize or get the state of my figure programmatically in MATLAB?

706 views (last 30 days)
I would like to maximize, minimize or restore my figure programmatically in MATLAB.
I also want to get the position of a maximized window to know where I can place figures on the screen.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 May 2020
Edited: MathWorks Support Team on 27 May 2020
MATLAB R2018a and later
Starting in MATLAB R2018a, you can use the "WindowState" property to maximize, minimize, or display a figure in full-screen mode.
This is the recommended approach for versions of MATLAB R2018a onward.
To maximize a window and retrieve its full position in the monitor:
>> f = figure('WindowState','maximized');
>> pause(1);
>> f.Position
Unfortunately if the "pause" command is not present inside the script, it returns the original position faster than the figure maximizes, so in your function, I recommend including this "pause" to ensure the full screen position is calculated. The length of the "pause" may need to be increased for older systems.
MATLAB R2017b and earlier
As a workaround for MATLAB 7.4 (R2007a) through MATLAB R2017b, the attached files maxfig.p, minfig.p and figstate.p (with help files maxfig.m, minfig.m and figstate.m) allow you to perform these actions as follows:
minfig(F,1) % Minimizes the figure window for the figure with handle F
minfig(F,0) % Restores figure F if F is minimized
maxfig(F,1) % Maximizes the figure window for the figure with handle F
maxfig(F,0) % Restores figure F if F is maximized
s = figstate(F) % Returns the state of figure { Maximized | Minimized | Normal }
Please note that querying the figure position immediately after using these functions may not work reliably unless the figure resize is complete.
There are no workarounds for versions prior to MATLAB 7.4 (R2007a).

  7 Comments

Show 4 older comments
Steve Grobler
Steve Grobler on 20 Jan 2017
"It is well past time that the Mathworks implemented a supported method to comprehensively control figure window minimisation/maximisation."
too true!

Sign in to comment.

More Answers (3)

Jan
Jan on 27 Sep 2014
Edited: Jan on 28 Mar 2018
In reply to Max Müller's comment: I'm not sure if this works under 2006a already, but it fails under R6.5:
jFrame = get(handle(FigureHandle), 'JavaFrame');
jFrame.setMaximized(1);
Other useful methods:
jFrame.setMaximized(0);
jFrame.setMinimized(1);
jFrame.isMaximized % Thanks Image Analyst
jFrame.isMinimized
Alternatively you can use FEX: WindowAPI under Windows.

  4 Comments

Show 1 older comment
Lennart van Luijk
Lennart van Luijk on 20 Aug 2015
By far the most useful answer, since this is a built-in function and the jFrame object allows dozens of other actions as well.
ori yungrise
ori yungrise on 17 Oct 2018
Great answer! Pay attention - It is important to wait between the two commands with "pause" function or another way. Like "Nikolay S" in the next answer.

Sign in to comment.



Conrad Andrews
Conrad Andrews on 15 Jun 2017
Potentially a solution that may help:
If you get the figure handle and find the position property for that figure, it can give you the exact location and size of the figure on your screen. Say for example if you want to save the figure automatically at a particular window size.
Figgy = gcf;
Figgy.Position
This returned:
ans =
2 42 958 954
Which is where I set my figure at on the screen with the exact window size I wanted. Now if I want to replicate this position for a second figure (to save it with the exact size), I get the figure handle for the second one but this time I set the Position value to those coordinates:
Figgy2 = gcf;
Figgy2.Position = [2, 42, 958, 954];
Then my new figure is in the exact size and location as my previous one. I hope this helps someone.

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!