自动最大化一个图形

40
我正在使用MATLAB创建一些图形并自动将它们保存到文件中。问题在于这些图像的尺寸都很小。手动解决这个问题的好方法是创建一个图像(figure),将其最大化,然后保存到文件中。
我错过了自动最大化图像的步骤。
有什么建议吗? 到目前为止,我只发现了这个:

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

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

但是没有一个解决我的问题。


3
不应将此标记为重复。 参考问题涉及将图形设置为特定大小(以像素为单位)。 该问题(和答案)将图形设置为最大尺寸,而不涉及所涉及的像素数量。 - Pursuit
11个回答

63
这对我有用:
figure('units','normalized','outerposition',[0 0 1 1])

或者对于当前图形:

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

我还使用了在FileExchange上使用Java编写的MAXIMIZE函数。这是真正的最大化。


我正在使用MATLAB 2014a,但这些解决方案对我无效。图形已经最大化了,但保存到文件中的图形纵横比仍然是默认值(在默认设置下,子图可能看起来非常小)。我发现在另一个MATLAB线程中指出的“PaperPosition”选项对我很有帮助。图形UI不会改变其纵横比,但输出图像质量很高! - HuaTham
@HuaTham:你也可以试试文件交换中心上的export_fig插件。它会将图像保存为屏幕上所见的样子。 - yuk

24

想要实现最大化窗口(就像在OS X和Windows的用户界面中点击最大化按钮一样),您可以尝试以下方法,调用一个隐藏的Java句柄:

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

pause(n) 是必要的,因为上述代码跳出了 Matlab 的范畴,并运行在一个独立的 Java 线程上。将 n 设置为任意值并检查结果。计算机执行速度越快,n 可以设置得越小。

这里可找到完整的“文档”.


运行正常,但会抛出以下警告:警告:JavaFrame图形属性将在未来版本中被删除。有关更多信息,请参见mathworks.com上的Java和ActiveX用户建议。 Matlab R2019b @ Win10 64位 - winkmal

15

从R2018a开始, figureuifigure 对象都包含一个叫做 WindowState 的属性。默认情况下,它被设置为 'normal',但将其设置为 'maximized' 可以得到期望的结果。

总之:

hFig.WindowState = 'maximized'; % Requires R2018a

此外,正如Unknown123的评论中提到的那样:
  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.

最后,针对你的原始问题 - 如果你想导出图形内容为图片而不必担心结果太小 - 我强烈推荐使用export_fig工具。

1
另外,在绘制任何内容之前,您可以将其设置为默认值,set(groot,'defaultFigureWindowState','maximized'); - Unknown123
1
或者将其设置为所有打开的图形:“set(get(groot,'Children'),'WindowState','maximized');” - Unknown123
请参阅 Figure Properties 文档 https://www.mathworks.com/help/matlab/ref/matlab.ui.figure-properties.html 中的 Window Appearance 部分,了解有关 WindowState 的更多信息。 - Unknown123

6
为了最大化窗口,您可以模仿实际使用的按键顺序:
  1. ALT-SPACE(如此处所示)来访问窗口菜单;然后
  2. X用于最大化(这可能在您的系统中有所不同)。
为了以编程方式发送按键,可以使用类似此答案的基于Java的过程,如下所示:
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

完成!窗口已最大化!


4

正如上面的作者所提出的那样, 如果你想模拟点击“最大化”窗口按钮,你可以使用下面的代码。与提到的答案不同的是,使用“drawnow”而不是“pause”似乎更正确。

figure;
% do your job here
drawnow;
set(get(handle(gcf),'JavaFrame'),'Maximized',1);

5
JavaFrame属性将在未来版本中被移除。请参见http://www.mathworks.com/support/contact_us/dev/javaframe.html。 - transversality condition

4

在我看来,将图形窗口最大化并不是以更高分辨率保存图像的最佳方式。

有关于打印和保存的图形属性。使用这些属性,您可以按照任何您想要的分辨率保存文件。为了保存文件,您必须使用print函数,因为您可以设置一个dpi值。所以,首先设置以下图形属性:

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

其次,将文件保存为png格式,并设置分辨率为100dpi(例如使用'-r100'参数)。

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

为了获得一个尺寸为 2048x1536px 的文件,请将 Width = 2048/100 和 Height 1536/100,因为您使用的是 100dpi 进行保存所以要除以 /100。如果您更改 dpi 值,您也必须将除数更改为相同的值。
如您所见,无需从文件交换或基于 Java 的程序中获取任何额外的函数。此外,您可以选择任何所需的分辨率。

2
您可以尝试这个:
screen_size = get(0, 'ScreenSize');
f1 = figure(1);
set(f1, 'Position', [0 0 screen_size(3) screen_size(4) ] );

此解决方案未考虑前景元素。例如,在我的Windows机器上,由于任务栏,屏幕尺寸大于窗口的最大尺寸。 - Shai

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

作为横截条件(见https://dev59.com/GWUp5IYBdhLWcg3wbXJf#96ygEYcBWogLw_1blvAq),这里应用的方法依赖于Matlab未来版本中将被删除的某些内容。此代码使用“warning('off',...”行抑制了Matlab对此的警告。 - TTT

0

这是最短的形式

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

2
能否再详细解释一下? - Sulthan Allaudeen
1
这个答案与 ifryed 的回答有什么不同? - Shai

0
我建议使用 set 命令来更改图形的 MenuBarToolbar 属性。由于它适用于更旧和更新版本的 Matlab,所以 set 命令更加灵活多用。
fig = figure(1);
set(fig, 'MenuBar', 'none');
set(fig, 'ToolBar', 'none');

现在,您可以再次使用set将您的图形设置为全屏。
set(fig, 'Position', get(0,'Screensize'));

请注意,如果您先将图像设置为全屏,然后再删除菜单栏和工具栏,则图像不会处于全屏状态,请确保按正确顺序运行这些操作。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接