加载中…
个人资料
ccpacer
ccpacer
  • 博客等级:
  • 博客积分:0
  • 博客访问:937,750
  • 关注人气:147
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
相关博文
推荐博文
谁看过这篇博文
加载中…
正文 字体大小:

如何将matlab的figure最大化(整理转载)

(2015-09-11 09:05:33)
标签:

房产

分类: Matlab
参考帖子:
(1)http://www.matlabsky.com/thread-32632-1-1.html
(2)http://dpinglee.blog.163.com/blog/static/14409775320117345216700/
(3)http://cn.mathworks.com/matlabcentral/fileexchange/10274-maximize-figure-windows

================================================================
其实,matlab里面没有一个现成的函数是做这个事情的!但是有下面变通的方法:

Matlab默认的绘图窗口是自动的,要想窗口初始最大化,有以下几种方法:

1.

figure;
set(gcf,'outerposition',get(0,'screensize'));

2.

a=get(0);
figure('position',a.MonitorPositions);

3.

set(gcf,'Position',get(0,'ScreenSize'))

总结:这里涉及到了MATLAB句柄的使用,以及"0”作为根句柄的规则。


================================================================
有时我们绘图希望figure直接最大化、甚至全屏,虽然用的不多,但是需要用来还真不是一个小问题哦!

(1)最简单的方法就是设置的figure的outerposition或者position属性为screensize,这个方法并非真正的
最大化,只是调整了figure的大小和坐标而已!
  1. sz=get(0,'screensize');
  2. figure('outerposition',sz);
复制代码
(2)地球人都知道,matlab是使用Java做框架和界面,于是只要从Java入手,应该可以毫无压力的搞定这些
日常操作:
  1. h=figure;
  2. jFrame=getJFrame(gcf); %或者jframe=getJFrame(h); 
  3. jFrame.setMaximized(1); %最大化figure
  4. jFrame.setMinimized(1); %最小化figure
  5. jFrame.setAlwaysOnTop(1); %将figure置顶
复制代码
需要用掉下面的getJFrame.m代码,具体可以查看这里,另外感谢junziyang编写getJFrame函数!
  1. function JFrame = getJFrame(hfig)
  2. error(nargchk(1,1,nargin));
  3. if ~ishandle(hfig) && ~isequal(get(hfig,'Type'),'figure')
  4.     error('The input argument must be a Figure handle.');
  5. end
  6. mde = com.MathWorks.mde.desk.MLDesktop.getInstance;
  7. if isequal(get(hfig,'NumberTitle'),'off') && isempty(get(hfig,'Name'))
  8.     figTag = 'junziyang'; %Name the figure temporarily
  9.     set(hfig,'Name',figTag);
  10. elseif isequal(get(hfig,'NumberTitle'),'on') && isempty(get(hfig,'Name'))
  11.     figTag = ['Figure ',num2str(hfig)];
  12. elseif isequal(get(hfig,'NumberTitle'),'off') && ~isempty(get(hfig,'Name'))
  13.     figTag = get(hfig,'Name');
  14. else
  15.     figTag = ['Figure ',num2str(hfig),': ',get(hfig,'Name')];
  16. end
  17. drawnow %Update figure window
  18. jfig = mde.getClient(figTag); %Get the underlying JAVA object of the figure.
  19. JFrame = jfig.getRootPane.getParent();
  20. if isequal(get(hfig,'Name'),'junziyang')
  21.     set(hfig,'Name',''); �lete the temporary figure name
  22. end
复制代码
(3)另外在调整figure窗体的位置时,我们可以能需要用到一个movegui()函数,这个函数的功能就是
将figure平移到某个位置,但是不调整figure窗口的大小,具体用法可以参考matlab的帮助。

(4)在Window中Figure无非就是一个窗口,显然我们可以通过Window API对这个窗口进行操作
  1. plot(1:5,1:5)
  2. maximize
复制代码
对应需要调用maximize.m函数以及DLL动态链接库(  maximize.zip (4.21 KB, 下载次数: 60) 
  1. function maximize(h)

  2. % MAXIMIZE   maximize figure windows
  3. % ====================================================================
  4. %
  5.       Berne University of Applied Sciences
  6. %
  7.       School of Engineering and Information Technology
  8.       Division of Electrical- and Communication Engineering
  9. %
  10. % ====================================================================
  11.                      maximize figure windows
  12. % ====================================================================
  13. %
  14. % Author:    Alain Trostel
  15. % e-mail:    alain.trostel@bfh.ch
  16. % Date:      June 2007
  17. % Version:   4.1
  18. %
  19. % ====================================================================
  20. %
  21. % function maximize(h)
  22. %
  23. % Input parameters
  24. % -----------------
  25.              handle(s) of the figure window
  26. %
  27. %
  28. % Output parameters
  29. % ------------------
  30.  The function has no output parameters.
  31. %
  32. %
  33. % Used files
  34. % -----------
  35.  - windowMaximize.dll
  36. %
  37. %
  38. % Examples
  39. % ---------
  40.  % maximize the current figure
  41.  ------------------------------
  42.  maximize;
  43. %
  44. %
  45.  % maximize the current figure
  46.  ------------------------------
  47.  maximize(gcf);
  48. %
  49. %
  50.  % maximize the specified figure
  51.  --------------------------------
  52.  h = figure;
  53.  maximize(h);
  54. %
  55. %
  56.  % maximize the application window
  57.  ----------------------------------
  58.  maximize(0);
  59. %
  60. %
  61.  % maximize more than one figure
  62.  --------------------------------
  63.  h(1) = figure;
  64.  h(2) = figure;
  65.  maximize(h);
  66. %
  67. %
  68.  % maximize all figures
  69.  -----------------------
  70.  maximize('all');
  71. %
  72. %
  73.  % maximize a GUI in the OpeningFcn
  74.  -----------------------------------
  75. %
  76.  % --- Executes just before untitled is made visible.
  77.  function untitled_OpeningFcn(hObject, eventdata, handles, varargin)
  78.  % This function has no output args, see OutputFcn.
  79.  % hObject    handle to figure
  80.  % eventdata  reserved - to be defined in a future version of MATLAB
  81.  % handles    structure with handles and user data (see GUIDATA)
  82.  % varargin   command line arguments to untitled (see VARARGIN)
  83. %
  84.  % Choose default command line output for untitled
  85.  handles.output = hObject;
  86. %
  87.  % Update handles structure
  88.  guidata(hObject, handles);
  89. %
  90.  % UIWAIT makes untitled wait for user response (see UIRESUME)
  91.  % uiwait(handles.figure1);
  92. %
  93.  % maximize the GUI
  94.  set(hObject,'Visible','on');
  95.  maximize(hObject);



  96. % check if dll-file exists
  97. if ~exist('windowMaximize.dll','file')
  98.     error('windowMaximize.dll not found.');
  99. end

  100. % if no input parameters, get handle of the current figure
  101. if nargin == 0
  102.     h = gcf;
  103. end

  104. % if one input parameter, check the input parameter
  105. if ischar(h)
  106.     % check the string
  107.     if strcmpi(h,'all')
  108.         % get all figure handles
  109.         h = findobj('Type','figure');            
  110.     else
  111.         % incorrect string argument
  112.         error('Argument must be the correct string.');
  113.     end
  114. else
  115.     % check each handle
  116.     for n=1:length(h)
  117.         % it must be a handle and of type 'root' or 'figure'
  118.         if ~ishandle(h(n)) || (~strcmp(get(h(n),'Type'),'root') && ...
  119.                                ~strcmp(get(h(n),'Type'),'figure'))
  120.             % incorrect handle
  121.             error('Argument(s) must be a correct handle(s).');
  122.         end
  123.     end
  124. end

  125. % if handle is not the root
  126. if h ~= 0
  127.     % for each handle
  128.     for n=length(h):-1:1
  129.         % create the temporary window name
  130.         windowname = ['maximize_',num2str(h(n))];

  131.         % save current window name
  132.         numTitle = get(h(n),'NumberTitle');
  133.         figName = get(h(n),'Name');

  134.         % set the temporary window name
  135.         set(h(n),'Name',windowname,'NumberTitle','off');

  136.         % draw figure now
  137.         drawnow;
  138.         % maximize the window with the C function
  139.         windowMaximize(windowname,get(h(n),'Resize'));

  140.         % reset the window name
  141.         set(h(n),'Name',figName,'NumberTitle',numTitle);
  142.     end
  143. else
  144.     % maximize the application window "MATLAB"
  145.     windowMaximize('MATLAB');
  146. end
复制代码
(5)这里还有一个通过API函数实现的Figure操作,  AlterWindow.zip (1.54 KB, 下载次数: 19) 
MEX-file to affect windows, e.g. maximize, minimize, hide, show, etc.

To compile use

>> mex AlterWindow.c user32.

See AlterWindow.m for help and a list of actions that can be performed.

0

阅读 评论 收藏 转载 喜欢 打印举报/Report
  • 评论加载中,请稍候...
发评论

    发评论

    以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

      

    新浪BLOG意见反馈留言板 电话:4000520066 提示音后按1键(按当地市话标准计费) 欢迎批评指正

    新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑

    新浪公司 版权所有