Search code examples
matlabstartupfigure

How to maximize a MATLAB GUI figure/window on startup?


I am currently evaluating GUIs in MATLAB and was wondering how to maximize a GUI window on startup, without the need for user interaction. The function I am using is stated hereafter and works fine if called on a button press, but calling it in the figure's OpeningFcn won't help.

http://www.mathworks.com/matlabcentral/fileexchange/25471-maximize

Any help on some startup section to place the function call, which is executed after the GUI window has been drawn? I searched for solutions related to startup code in MATLAB GUIs, but there were no results to date. Thanks in advance for your efforts.


Solution

  • Since many people seem to be interested in this and there is still no public solution, I will describe my approach:

    1. In the YourGUIName_OpeningFcn(hObject, eventdata, handles, varargin) function, append the following lines:

      % Initialize a timer, which executes its callback once after one second
      timer1 = timer('Period', 1, 'TasksToExecute', 1, ...
                'ExecutionMode', 'fixedRate', ...
                'StartDelay', 1);
      % Set the callback function and declare GUI handle as parameter
      timer1.TimerFcn = {@timer1_Callback, findobj('name', 'YourGUIName')};
      timer1.StopFcn = @timer1_StopFcn;
      start(timer1);
      
    2. Declare the timer1_Callback and timer1_StopFcn functions:

      %% timer1_Callback        
      % --- Executes after each timer event of timer1.
      function timer1_Callback(obj, eventdata, handle)
      
      % Maximize the GUI window
      maximize(handle);
      
      %% timer1_StopFcn        
      % --- Executes after timer stop event of timer1.
      function timer1_StopFcn(obj, eventdata)
      
      % Delete the timer object
      delete(obj);
      
    3. Declare the maximize function:

      function maximize(hFig)
      %MAXIMIZE: function which maximizes the figure withe the input handle
      %   Through integrated Java functionality, the input figure gets maximized
      %   depending on the current screen size.
      
      if nargin < 1
          hFig = gcf;             % default: current figure
      end
      drawnow                     % required to avoid Java errors
      jFig = get(handle(hFig), 'JavaFrame'); 
      jFig.setMaximized(true);
      end
      

    Source of the maximize function:

    http://www.mathworks.com/matlabcentral/fileexchange/25471-maximize