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

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

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.
Please run the below command in the MATLAB R2018a command window to get the release specific documentation:
web(fullfile(docroot, 'matlab/ref/matlab.ui.figure-properties.html#d119e305946'))
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).
Please follow the below link to search for the required information regarding the current release:
https://www.mathworks.com/help/

4 Comments

Try calling this function:
% Function to maximize the window via undocumented Java call.
% Reference: https://undocumentedmatlab.com/articles/minimize-maximize-figure-window
function MaximizeFigureWindow()
try
FigurejFrame = get(handle(gcf),'JavaFrame');
FigurejFrame.setMaximized(true);
catch ME
errorMessage = sprintf('Error in function %s() at line %d.\n\nError Message:\n%s', ...
ME.stack(1).name, ME.stack(1).line, ME.message);
fprintf(1, '%s\n', errorMessage);
uiwait(warndlg(errorMessage));
end
return; % from MaximizeFigureWindow()
Comment by Ben Stanley moved here:
There are serious problems with maxfig.p (I can't vouch for minfig.p):
  1. It causes race conditions. maxfig.p returns before the figure is maximised, so any code following must wait until the window is actually resized, particularly if it is creating a fixed size GUI depending upon the window size.
  2. It causes tooltips and popup menus to appear in the wrong place (observed on Matlab R2014b on RHEL7). The error appears to be related to the position of the figure window before maximising.
Therefore maxfig.p is an unacceptable workaround, introducing more problems than it solves.
This problem has been around for a long time. The alternative solutions of messing with the JavaFrame cause deprecation warnings (see https://undocumentedmatlab.com/articles/minimize-maximize-figure-window ). It is well past time that the Mathworks implemented a supported method to comprehensively control figure window minimisation/maximisation.

Sign in to comment.

More Answers (1)

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.

Categories

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

Products

Community Treasure Hunt

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

Start Hunting!