MATLAB update waitbar

I am trying to implement a "percent complete" bar in a MATLAB program, using the waitbar function. However, I am having trouble with it. Here is the code that I have currently:

in my GUI

POSITION = [53.3333 20 188.5446 20];   
H = uiwaitbar(POSITION); 
for percentageDone = 0;


function h = uiwaitbar(varargin)

if ishandle(varargin{1}) 
    ax = varargin{1}; 
    value = varargin{2}; 
    p = get(ax,'Child'); 
    x(3:4) = value; 

pos = varargin{1}; 
bg_color = [1 1 1]; 
fg_color = [0 .5 0]; 
h = axes('Units','pixels',... 
    'XLim',[0 100],'YLim',[0 1],... 
patch([0 0 0 0],[0 1 1 0],fg_color,... 


Elsewhere in the script, I have a KeyPressFcn callback, in which the user inputs the answer to their questions. At the end of this callback, for every correct answer I want the waitbar to fill up a little. However, no matter what values I assign to percentageDone variable the waitbar in the GUI does not change at all.

Can anybody help me with this?


I'm confused, you say you are using the builtin function WAITBAR, but then you seem to be implementing one yourself..

Anyway, here is a rather useless example that shows a custom progress bar. Just keep pressing "next" :)

function progressBarDemo()
    %# a figure and a plot area
    hFig = figure('Menubar','none');
    hAxPlot = axes('Parent',hFig, 'Box','on', ...
        'Units','normalized', 'Position',[0.1 0.2 0.8 0.6]);
    hLine = line('Parent',hAxPlot, 'XData',1:1000, 'YData',nan(1,1000), ...

    %# next button
    uicontrol('Style','pushbutton', 'String','next', ...

    %# progress bar axis
    x = linspace(0, 1, 13+1);            %# steps
    hAx = axes('Parent',hFig, 'XLim',[0 1], 'YLim',[0 1], ...
        'XTick',[], 'YTick',[], 'Box','on', 'Layer','top', ...
        'Units','normalized', 'Position',[0 0.9 1 0.1]);
    hPatch = patch([0 0 x(1) x(1)], [0 1 1 0], 'r', 'Parent',hAx, ...
        'FaceColor','r', 'EdgeColor','none');
    hText = text(0.5, 0.5, sprintf('%.0f%%',x(1)*100), ...
        'Parent',hAx, 'Color','w', 'BackgroundColor',[.9 .5 .5], ...
        'HorizontalAlign','center', 'VerticalAlign','middle', ...
        'FontSize',16, 'FontWeight','bold');
    counter = 2;

    %# next button callback function
    function buttonCallback(src,evt)
        %# draw another random plot
        set(hLine, 'YData',cumsum(rand(1000,1)-0.5))

        %# update progress bar
        set(hPatch, 'XData',[0 0 x(counter) x(counter)])
        set(hText, 'String',sprintf('%.0f%%',x(counter)*100))

        %# terminate if we have reached 100%
        counter = counter + 1;
        if counter > numel(x)
            set(src, 'Enable','off', 'String','Done')


You're probably just missing a drawnow call after setting the XData property, to force a flush of the graphics events queue. If this does not fix your problem, then include enough code to reproduce the symptoms.

Have you tried using Progressbar from the File Exchange? It might save you a lot of hassle. I've always had good results with it.

Do you first create the waitbar? Something like this:

h = waitbar(0, '1', 'Name', 'My progress bar', 'CreateCancelBtn', 'setappdata(gcbf, ''canceling'', 1)');

After that, to update the waitbar:

Edit: fixed bug with text output: percentageDone must be multiplied by 100.

waitbar(percentageDone, h, sprintf('Already %d percent ready!', 100*percentageDone));


 ? xlsread and waitbar MATLAB
 ? How to modify xlsread to get a step-by-step updated flag from loading process
 ? How can I make waitbar in MATLAB modal
 ? Get waitbar value in Matlab
 ? waitbar matlab before start a standalone script
 ? matlab progress bar not depanding on time
 ? JavaFX ProgressBar/ProgressIndicator: listen for change from multiple Sliders
 ? JavaFX ProgressBar/ProgressIndicator: listen for change from multiple Sliders
 ? JavaFX ProgressBar/ProgressIndicator: listen for change from multiple Sliders
 ? JavaFX ProgressBar: how to add an animation?