【问题标题】:Automatically maximize a figure自动最大化图形
【发布时间】:2013-02-23 13:15:44
【问题描述】:

我正在 MATLAB 中创建一些图形并自动将它们保存到文件中。根据定义,图像很小的问题。手动解决我的问题的一个好方法是创建一个图像(图),将其最大化,然后保存到一个文件中。

我错过了自动最大化图形的这一步。

有什么建议吗? 到目前为止,我只发现了这个:

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

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

但没有人能解决我的问题。

【问题讨论】:

  • 这不应被标记为重复。参考问题是指将图形设置为特定大小,以像素为单位。这个问题(和答案)将图形设置为最大尺寸,而不参考所涉及的像素数。

标签: matlab export matlab-figure maximize-window


【解决方案1】:

这对我有用:

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

或当前图:

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

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

【讨论】:

  • 我使用的是 MATLAB 2014a,这些解决方案对我不起作用。该图已最大化,但保存到文件中的图的方面仍然是默认方面(并且子图在默认设置中看起来非常小)。我发现another MATLAB thread 中指出的设置“PaperPosition”的选项对我有帮助。图形 UI 不会改变其外观,但输出图像质量很高!
  • @HuaTham:您也可以尝试在 File Exchange 上提交export_fig。它保存的图像与您在屏幕上看到的完全一样。
【解决方案2】:

你可以试试这个:

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

【讨论】:

  • 此解决方案不考虑前景元素。例如,在我的 windows 机器上,由于任务栏,屏幕尺寸大于窗口的最大尺寸。
【解决方案3】:

对于实际的最大化(就像在 OS X 和 Windows 的 UI 中单击最大化按钮一样) 您可以尝试以下调用隐藏 Java 句柄的方法

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

pause(n) 是必不可少的,因为上述内容超出了 Matlab 的范围,并且位于单独的 Java 线程上。将n 设置为任意值并检查结果。计算机在执行时越快,n 就越小。

完整的“文档”可以在here找到

【讨论】:

  • 工作正常,但会引发以下警告: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
【解决方案4】:

这是最短的格式

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

【讨论】:

  • 介意再解释一下吗?
  • 这个答案与ifryed's有什么不同
【解决方案5】:

As it is proposed by an author above,如果你想模拟点击“最大化”窗口按钮,可以使用下面的代码。与提到的答案不同的是,使用“drawnow”而不是“pause”似乎更正确。

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

【讨论】:

【解决方案6】:

要最大化图形,您可以模仿实际使用的按键顺序:

  1. ALT-SPACE(如here所示)访问窗口菜单;然后
  2. X 最大化(这可能因您的系统而异)。

要以编程方式发送密钥,您可以使用类似于this answer 的基于 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

瞧!窗口最大化!

【讨论】:

    【解决方案7】:

    恕我直言,最大化图形窗口并不是将图形保存为更高分辨率图像的最佳方式。

    printing and saving 有图形属性。使用这些属性,您可以以所需的任何分辨率保存文件。要保存文件,您必须使用print function,因为您可以设置dpi 值。所以,首先设置下图属性:

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

    然后将文件(例如)另存为 100dpi 的 png ('-r100')

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

    要获取带有2048x1536px 设置Width = 2048/100 和高度1536/100/100 的文件,因为您以100dpi 保存。如果您更改 dpi 值,您还必须将除数更改为相同的值。

    正如您所见,文件交换或基于 java 的过程不需要任何额外的功能。此外,您可以选择任何所需的分辨率。

    【讨论】:

      【解决方案8】:
      %% 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
      

      【讨论】:

      • 作为横向条件noted,此处应用的方法依赖于将在未来版本的 Matlab 中删除的内容。此代码使用 warning('off',... 行抑制 Matlab 对此的警告。
      【解决方案9】:

      As of R2018afigure 以及 uifigure 对象包含一个名为 WindowState 的属性。默认情况下设置为'normal',但将其设置为'maximized' 可以获得所需的结果。

      总结:

      hFig.WindowState = 'maximized'; % Requires R2018a
      

      此外,正如Unknown123 的 cmets 中所述:

      1. 可以使用以下方法使图形默认最大化:

        set(groot, 'defaultFigureWindowState', 'maximized');
        
      2. 使用以下方法可以最大化所有打开的图形:

        set(get(groot, 'Children'), 'WindowState', 'maximized');
        
      3. 有关'WindowState' 以及控制图形外观的其他属性的更多信息,请参见this documentation page

      最后,为了解决您最初的问题 - 如果您想将图形的内容导出为图像而不必担心结果太小 - 我强烈推荐 export_fig 实用程序。

      【讨论】:

      • 另外,您可以在绘制任何内容之前将其设置为默认值,set(groot, 'defaultFigureWindowState', 'maximized');
      • 或者为所有打开的人物设置它set( get(groot, 'Children'), 'WindowState', 'maximized');
      • 有关WindowState 的更多信息,请参阅Window Appearance 部分中的Figure Properties 文档mathworks.com/help/matlab/ref/matlab.ui.figure-properties.html
      【解决方案10】:

      我建议使用set 命令来更改图形的MenuBarToolbar 属性。 set 命令更加通用,因为它适用于较旧和较新版本的 Matlab。

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

      现在您可以再次使用set 使您的人物全屏显示。

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

      请注意,如果先让图形全屏,然后删除菜单栏和工具栏,图形将不会全屏,因此请确保以正确的顺序运行。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-08-12
        • 2015-01-18
        • 2011-12-12
        • 1970-01-01
        • 2011-05-17
        • 2019-06-19
        • 1970-01-01
        • 2014-01-18
        相关资源
        最近更新 更多