40

I am creating some figures in MATLAB and automatically save them to files. The problem that by definition the images are small. A good way to solve my problem by hand is to create an image (figure), maximize it, and save to a file.

I am missing this step of automatically maximize a figure.

Any suggestions? Up till now I only found this:

http://answers.yahoo.com/question/index?qid=20071127135551AAR5JYh

http://www.mathworks.com/matlabcentral/newsreader/view_thread/238699

but none are solving my problem.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
  • 3
    This should not be marked as duplicate. The references question refers to setting a figure to a specific size, in pixels. This question (and answer) set a figure to the maximum size, without reference to the number of pixels involved. – Pursuit Aug 06 '13 at 21:18

11 Answers11

63

This worked for me:

figure('units','normalized','outerposition',[0 0 1 1])

or for current figure:

set(gcf,'units','normalized','outerposition',[0 0 1 1])

I have also used MAXIMIZE function on FileExchange that uses java. This is true maximization.

yuk
  • 19,098
  • 13
  • 68
  • 99
  • I am using MATLAB 2014a, and these solutions do not work for me. The figure is maximized alright, but the aspect of the figure saved onto the file is still in the default aspect (and the subplots can look ridiculously small in the default setting). I find the option of setting 'PaperPosition', pointed out in [another MATLAB thread](https://www.mathworks.com/matlabcentral/newsreader/view_thread/131297), to be helpful for me. The figure UI will not change its aspect but the output image is in high quality! – HuaTham Oct 22 '14 at 22:48
  • @HuaTham: You can also try [export_fig](http://www.mathworks.com/matlabcentral/fileexchange/23629-export-fig) submission on the File Exchange. It saves the image exactly as you see it on the screen. – yuk Oct 23 '14 at 02:48
24

For an actual Maximize (exactly like clicking the maximize button in the UI of OS X and Windows) You may try the following which calls a hidden Java handle

figure;
pause(0.00001);
frame_h = get(handle(gcf),'JavaFrame');
set(frame_h,'Maximized',1);

The pause(n) is essential as the above reaches out of the Matlab scape and is situated on a separate Java thread. Set n to any value and check the results. The faster the computer is at the time of execution the smaller n can be.

Full "documentation" can be found here

The-Duck
  • 501
  • 5
  • 9
  • Works fine, but throws the following warning: `Warning: The JavaFrame figure property will be removed in a future release. For more information, see Recommendations for Java and ActiveX Users on mathworks.com.` Matlab R2019b @ Win10 64bit – winkmal Jul 21 '20 at 10:22
15

As of R2018a, figure as well as uifigure objects contain a property called WindowState. This is set to 'normal' by default, but setting it to 'maximized' gives the desired result.

In conclusion:

hFig.WindowState = 'maximized'; % Requires R2018a

Furthermore, as mentioned in Unknown123's comments:

  1. Making figures maximized by default is possible using:

    set(groot, 'defaultFigureWindowState', 'maximized');
    
  2. Maximizing all open figures is possible using:

    set(get(groot, 'Children'), 'WindowState', 'maximized');
    
  3. More information about 'WindowState' as well as other properties controlling figure appearance can be found in this documentation page.

Finally, to address your original problem - if you want to export the contents of figures to images without having to worry about the results being too small - I would highly recommend the export_fig utility.

Dev-iL
  • 23,742
  • 7
  • 57
  • 99
  • 1
    also, you could set it to default before plotting anything, `set(groot, 'defaultFigureWindowState', 'maximized');` – Unknown123 Nov 22 '18 at 03:46
  • 1
    Or set it for all of the open figures `set( get(groot, 'Children'), 'WindowState', 'maximized');` – Unknown123 Nov 22 '18 at 03:50
  • See `Figure Properties` documentation https://www.mathworks.com/help/matlab/ref/matlab.ui.figure-properties.html in the `Window Appearance` section for more information about `WindowState` – Unknown123 Nov 22 '18 at 03:53
6

To maximize the figure, you can mimic the sequence of keys you would actually use:

  1. ALT-SPACE (as indicated here) to access the window menu; and then
  2. X to maximize (this may vary in your system).

To send the keys programmatically, you can use a Java-based procedure similar to this answer, as follows:

h = figure;                                          %// create figure and get handle
plot(1:10);                                          %// do stuff with your figure
figure(h)                                            %// make it the current figure
robot = java.awt.Robot; 
robot.keyPress(java.awt.event.KeyEvent.VK_ALT);      %// send ALT
robot.keyPress(java.awt.event.KeyEvent.VK_SPACE);    %// send SPACE
robot.keyRelease(java.awt.event.KeyEvent.VK_SPACE);  %// release SPACE
robot.keyRelease(java.awt.event.KeyEvent.VK_ALT);    %// release ALT
robot.keyPress(java.awt.event.KeyEvent.VK_X);        %// send X
robot.keyRelease(java.awt.event.KeyEvent.VK_X);      %// release X

Voilà! Window maximized!

Community
  • 1
  • 1
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
4

As it is proposed by an author above, if you want to simulate clicking the "maximize" windows button, you can use the code that follows. The difference with the mentionned answer is that using "drawnow" instead of "pause" seems more correct.

figure;
% do your job here
drawnow;
set(get(handle(gcf),'JavaFrame'),'Maximized',1);
Community
  • 1
  • 1
lnstadrum
  • 550
  • 3
  • 15
4

imho maximizing the figure window is not the best way to save a figure as an image in higher resolution.

There are figure properties for printing and saving. Using these properties you can save files in any resolution you want. To save the files you have to use the print function, because you can set an dpi value. So, firstly set the following figure properties:

set(FigureHandle, ...
    'PaperPositionMode', 'manual', ...
    'PaperUnits', 'inches', ...
    'PaperPosition', [0 0 Width Height])

and secondly save the file (for example) as png with 100dpi ('-r100')

print(FigureHandle, Filename, '-dpng', '-r100');

To get a file with 2048x1536px set Width = 2048/100 and Height 1536/100, /100 because you save with 100dpi. If you change the dpi value you also have to change the divisor to the same value.

As you can see there is no need for any extra function from file exchange or java-based procedure. In addition you can choose any desired resolution.

serial
  • 447
  • 4
  • 12
2

you can try this:

screen_size = get(0, 'ScreenSize');
f1 = figure(1);
set(f1, 'Position', [0 0 screen_size(3) screen_size(4) ] );
Shai
  • 111,146
  • 38
  • 238
  • 371
ifryed
  • 605
  • 9
  • 21
  • this solution does not take into account foreground elements. For instance, on my windows machine, the screen size is larger that the max size of window due to the taskbar. – Shai Jan 14 '15 at 11:53
1
%% maximizeFigure
%
% Maximizes the current figure or creates a new figure. maximizeFigure() simply maximizes the 
% current or a specific figure
% |h = maximizeFigure();| can be directly used instead of |h = figure();|
%
% *Examples*
%
% * |maximizeFigure(); % maximizes the current figure or creates a new figure|
% * |maximizeFigure('all'); % maximizes all opened figures|
% * |maximizeFigure(hf); % maximizes the figure with the handle hf|
% * |maximizeFigure('new', 'Name', 'My newly created figure', 'Color', [.3 .3 .3]);|
% * |hf = maximizeFigure(...); % returns the (i.e. new) figure handle as an output|
%
% *Acknowledgements*
% 
% * Big thanks goes out to Yair Altman from http://www.undocumentedmatlab.com/
%
% *See Also*
% 
% * |figure()|
% * |gcf()|
%
% *Authors*
%
% * Daniel Kellner, medPhoton GmbH, Salzburg, Austria, 2015-2017
%%

function varargout = maximizeFigure(varargin)

warning('off', 'MATLAB:HandleGraphics:ObsoletedProperty:JavaFrame')

% Check input variables
if isempty(varargin)
    hf = gcf; % use current figure
elseif strcmp(varargin{1}, 'new')
    hf = figure(varargin{2:end});
elseif strcmp(varargin{1}, 'all')
    hf = findobj('Type', 'figure');
elseif ~isa(varargin{1}, 'char') && ishandle(varargin{1}) &&...
        strcmp(get(varargin{1}, 'Type'), 'figure')
    hf = varargin{1};
else
    error('maximizeFigure:InvalidHandle', 'Failed to find a valid figure handle!')
end

for cHandle = 1:length(hf)   
    % Skip invalid handles and plotbrowser handles
    if ~ishandle(cHandle) || strcmp(get(hf, 'WindowStyle'), 'docked') 
        continue
    end

    % Carry the current resize property and set (temporarily) to 'on'
    oldResizeStatus = get(hf(cHandle), 'Resize');
    set(hf(cHandle), 'Resize', 'on');

    % Usage of the undocumented 'JavaFrame' property as described at:
    % http://undocumentedmatlab.com/blog/minimize-maximize-figure-window/
    jFrame = get(handle(hf(cHandle)), 'JavaFrame');

    % Due to an Event Dispatch thread, the pause is neccessary as described at:
    % http://undocumentedmatlab.com/blog/matlab-and-the-event-dispatch-thread-edt/
    pause(0.05) 

    % Don't maximize if the window is docked (e.g. for plottools)
    if strcmp(get(cHandle, 'WindowStyle'), 'docked')
        continue
    end

    % Don't maximize if the figure is already maximized
    if jFrame.isMaximized
        continue
    end

    % Unfortunately, if it is invisible, it can't be maximized with the java framework, because a
    % null pointer exception is raised (java.lang.NullPointerException). Instead, we maximize it the
    % straight way so that we do not end up in small sized plot exports.
    if strcmp(get(hf, 'Visible'), 'off')
        set(hf, 'Units', 'normalized', 'OuterPosition', [0 0 1 1])
        continue
    end

    jFrame.setMaximized(true);

    % If 'Resize' will be reactivated, MATLAB moves the figure slightly over the screen borders. 
    if strcmp(oldResizeStatus, 'off')
        pause(0.05)
        set(hf, 'Resize', oldResizeStatus)
    end
end

if nargout
    varargout{1} = hf;
end
braggPeaks
  • 1,158
  • 10
  • 23
  • As transversality condition [noted](https://stackoverflow.com/questions/15286458/automatically-maximize-figure-in-matlab#comment52208701_30704764), the method being applied here relies on something that will be removed in a future release of Matlab. This code suppresses Matlab's warning about this with the `warning('off',...` line. – TTT Nov 08 '17 at 20:21
0

This is the shortest form

figure('Position',get(0,'ScreenSize'))
0

I recommend the set command to change MenuBar and Toolbar properties of your figure. The set command is more versatile because it works for older and newer versions of Matlab.

fig = figure(1);
set(fig, 'MenuBar', 'none');
set(fig, 'ToolBar', 'none');

Now you can use set again to make your figure full screen.

set(fig, 'Position', get(0,'Screensize'));

Note that if you make the figure full screen first, and then remove the MenuBar and Toolbar, the figure will not be full screen, so make sure to run these in the correct order.

juju89
  • 554
  • 2
  • 5
  • 18
0

After MATLAB R2018a to programatically maximize my figures in MATLAB I use the WindowState property as explained here under the Window Appearance section

fig = figure('WindowState', 'maximized')

you can also do

fig = figure;
fig.WindowState = 'maximized';
VMMF
  • 906
  • 1
  • 17
  • 28