Search code examples
matlabprogress-barprogress

Disable progress bar Matlab


I'm using a code in Matlab composed of a large number of nested functions. A large number of these functions show progressbars. Is there any Matlab command or any possibility to disable progressbars showing, without having to locate and comment/remove all the lines where they are called from?


Solution

  • I assume from your comments you mean you have lots of functions calling waitbar.

    You could overload the 'waitbar' function with your own waitbar.m ensuring its higher on the searchpath. Although this is not usually a good idea and may cause problems in the future when you (or anyone else you uses your codes) do want to use the waitbar and it doesn't appear.

    Another (preferable in my view) way to disable it is to create your own intermediate function where you can toggle on/off the waitbar:

    function h = mywaitbar ( varargin )
      % preallocate output
      h = [];
      % use an internal persistent variable
      persistent active
      % by default set to true
      if isempty ( active ); active = true; end
      % Check to see if its a control call
      if nargin == 1 && ischar ( varargin{1} )
        % is it a call to disable it?
        if strcmp ( varargin{1}, '**disable**' )
          active = false;
        else 
          active = true;
        end
        return
      end
      if active 
        h = waitbar ( varargin{:} );
      end
    end      
    

    The downside to this is that you will need to find and replace all your waitbar commands with the new function 'waitbar', but this is a one time only action.

    Then disable all future calls to waitbar by:

     mywaitbar ( '**disable**' )
    

    Run your codes and no waitbar will be shown. The use of a peristent variable will keep the status until you restart Matlab (or you invoke clear all). To stop 'clear all' resetting it you can use mlock in the function.

    To reenable the waitbar:

     mywaitbar ( '**enable**' )
    

    To test it use the following code:

    for ii=1:10
      h = mywaitbar ( ii );
      fprintf ( 'test with waitbar %i\n', ii);
    end
    

    Now disable the waitbar capability:

    mywaitbar ( '**disable**' )
    for ii=1:10
      h = mywaitbar ( ii );
      fprintf ( 'test with waitbar disabled %i\n', ii);
    end
    

    You will see that the code above runs with no waitbar being shown.