Q:

Updating MATLAB waitbar fails after first update

I am having a problem updating a waitbar in a MATLAB GUI. 
I created a simple example that works as expected.
steps = 5;
hWaitBar = waitbar(0, 'Testing...');
for i = 1:steps
   waitbar(i/steps, hWaitBar);
   pause(1);
end
close(hWaitBar);

However when I use this construction in the GUI...
numSteps = %calculated
hWaitBar = waitbar(0, 'Processing...');

if %conditional
    for i = 1:numSteps
        waitbar(i/numSteps, hWaitBar)
        % additional processing
    end
else %conditional
    % additional processing
end

close(hWaitBar);

...the waitbar only displays correctly for the first for loop iteration. 
The second interation fails with the execption:
Error using waitbar(109)
Improper arguments for waitbar.

I have verified that the waitbar progress value does not exceed 1.
I have verified that the waitbar is not being closed until outside the if/else loop.

A:

I found the solution -- I was misuing handle graphics.
In my original code:
numSteps = %calculated 
hWaitBar = waitbar(0, 'Processing...'); 

if %conditional 
    for i = 1:numSteps 
        waitbar(i/numSteps, hWaitBar) 
        % additional processing
        % *** call to imagesc caused the error
    end 
else %conditional 
    % additional processing 
end 

close(hWaitBar); 

Using the debugger, I saw that the waitbar became the current figure and imagesc tried to plot to it instead of the axis on the main form. Setting the appropriate figure as current immediately before the referencing calls yielded proper behavior.
Corrected code:
numSteps = %calculated 
hForm = gcf; % save the handle of the main form
hWaitBar = waitbar(0, 'Processing...'); 

if %conditional 
    for i = 1:numSteps 
        % set the waitbar to be the current figure before it is updated
        % note: this syntax will ensure window order will be preserved
        %       with waitbar on top
        set(0, 'CurrentFigure', hWaitBar); 
        waitbar(i/numSteps, hWaitBar); 

        % additional processing
        set(0, 'CurrentFigure', hForm);
        imagesc(...);
        % more processing
    end 
else %conditional 
    % additional processing 
end 

close(hWaitBar); 

