Salvador Dali
Salvador Dali

Reputation: 222531

Automatically maximize a figure

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.

Upvotes: 40

Views: 105765

Answers (12)

cui xingxing
cui xingxing

Reputation: 301

The current recommended method for maximizing the Figure window with as few changes as possible to the inherent properties of the existing configure is as follows:

  • For creating a new Figure
g = groot;
figure('outerposition',[0,0,g.ScreenSize(3:4)+1])
  • For existing Figure
g = groot;
set(gcf,'outerposition',[0,0,g.ScreenSize(3:4)+1])

Upvotes: 0

VMMF
VMMF

Reputation: 954

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';

Upvotes: 0

juju89
juju89

Reputation: 549

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.

Upvotes: 0

Dev-iL
Dev-iL

Reputation: 24169

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.

Upvotes: 15

serial
serial

Reputation: 437

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.

Upvotes: 4

braggPeaks
braggPeaks

Reputation: 1186

%% 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

Upvotes: 1

lnstadrum
lnstadrum

Reputation: 576

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

Upvotes: 4

Luis Mendo
Luis Mendo

Reputation: 112669

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!

Upvotes: 6

ifryed
ifryed

Reputation: 605

you can try this:

screen_size = get(0, 'ScreenSize');
f1 = figure(1);
set(f1, 'Position', [0 0 screen_size(3) screen_size(4) ] );

Upvotes: 2

user48711
user48711

Reputation: 11

This is the shortest form

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

Upvotes: 0

The-Duck
The-Duck

Reputation: 509

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

Upvotes: 24

yuk
yuk

Reputation: 19870

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.

Upvotes: 63

Related Questions