Automatically maximize a figure

女生的网名这么多〃 提交于 2019-11-27 06:41:00

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.

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

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

By the way, 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.

Luis Mendo

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!

HomePlaneR

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

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.

ifryed

you can try this:

screen_size = get(0, 'ScreenSize');
f1 = figure(1);
set(f1, 'Position', [0 0 screen_size(3) screen_size(4) ] );
%% 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

This is the shortest form

figure('Position',get(0,'ScreenSize'))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!