tags:

views:

69

answers:

5

I want to have a waitbar for an operation that takes quite a while. Here is my code:

h = waitbar(0,'Please wait...');

for i=1:counterend
    waitbar(i/waitbarcounter)
    Atemp    = At+i*step;
    handle   = @(M) 1/M^2*((2/(gamma+1))*(1+(gamma-1)*M^2/2))^((gamma+1)/(gamma-1))-(Atemp/At)^2;
    Mach     = fzero(handle, 5);
    Aplot(i) =  Atemp/At;
    Tplot(i) = Tc / (1+(gamma-1)*Mach^2/2);
    Mplot(i) = Mach;
    plot(Aplot, Tplot)
end

close(h) 

The error Matlab gives is:

??? Error using ==> waitbar at 249
Improper arguments for waitbar

After investigation, I am sure that this error must occur because of the sorrounding code in the loop.

Note: The loop works fine without the waitbar.

A: 

I have checked waitbar on R2008b. So far, the only ways I was able to reproduce your error was by having i/counterend evaluate to an array with multiple rows (a 1x2 vector gives interesting results), and by closing the waitbar before calling waitbar(i/counterend).

I do not get any error running the following:

h = waitbar(0,'Please wait...');
counterend = 1000;
for i=1:counterend
    waitbar(i/counterend)
end
close(h)

Could you make sure that the small example above runs without error? If yes, please check that the waitbar is not closed during the execution of the loop, and that counterend is a scalar (use dbstop if error to stop execution of your code at the time of the error).

If the above example does not work without error, you should use which waitbar to check that you are using Matlab's waitbar, and not any of the many updated version from the Matlab File Exchange.

Jonas
Thanks for your help. I definitely use Matlab's waitbar.Running the example works fine. Following your hints I checked "counterend"s filetype and it turned out it was of the type double. Casting it to "waitbarcounter = uint8(counterend)" does not help as well, though.On a side note: After restarting Matlab this morning it suddenly stopped giving me an overview of the variables. See [here](http://gerth-ac.de/matlab_screen.png). Any help on that?
Ingo
A: 

Running

counterned=1000;
h = waitbar(0,'Please wait...');

for i=1:counterend
    waitbar(i/counterend)
end

close(h)

works perfectly as expected on MATLAB R2009a on Windows XP.

AndyL
A: 

The above runs fine on R2008a on XP also.

However, you get the error you describe if you kill the waitbar window before the next waitbar command comes around. If you want to be nice about it you should check if the handle h is still valid before issuing waitbar.

Matt Mizumi
Thanks for the advice, this is apparently not where the error lies though.
Ingo
+1  A: 

I prefer to use progressbar written by Steve Hoelzer on the MATLAB FEX. I haven't had any problems with it.

Doresoom
+1  A: 

Running

counterend = 10000;
>> h = waitbar(0,'Please wait...');

for i=1:counterend
    waitbar(i/counterend)
end

close(h);

Works as expected on 2007a / Windows XP.

On a side note, it would help knowing what countered is defined as. Something quick to check would be to ensure that you are not passing it a CELL.

Running

counterend = {10000};
h = waitbar(0,'Please wait...');

for i=1:counterend
    waitbar(i/counterend)
end

close(h);

Yields a different error (see below) in 2007a, but this error message may have changed in 2008.

??? Undefined function or method '_colonobj' for input arguments of type 'cell'.

My last bit of advice would be caution you on the use of waitbar for large arrays/data sets. While I think it is important to inform the user of the progress, to me there is also a concern for how much time is added to the loop. Working with arrays that have 100k+ entries, I became a religious user of the Profiler to see where the time was really being spent. I can tell you the time is not in the calculation of the i/X, it was all in updating the waitbar's display. To soften the blow of the update/drawnow, I only updated the waitbar every 100 to 1000 entry which helped tremendously.

EDIT: Updated response to match latest code

I first started to attack this problem at the anonymous function, having problems with them in the past it's a personal vendetta of mine. When looking into the function I found that you are using gamma, do you have this defined as a constant (constant to the loop / function)? The reason I ask is that 'gamma' is a Matlab function and was giving me errors when trying to run your function by itself. Below I have modified you code slightly and this does run fine here. If any of the assumptions I have made are wrong please let me know. In addition, if you did intend to use the gamma function, your function is missing any arguments to it. Hope this helps!

clc
h = waitbar(0,'Please wait...');
counterend = 1000;
waitbarcounter = counterend;
g_amma = 7;
At = 34;
step = 2;
Tc = 42;

for i=1:counterend
    waitbar(i/waitbarcounter)
    Atemp    = At+i*step;
    handle   = @(M) 1/M^2*((2/(g_amma+1))*(1+(g_amma-1)*M^2/2))^((g_amma+1)/(g_amma-1))-(Atemp/At)^2;
    Mach     = fzero(handle, 5);
    Aplot(i) =  Atemp/At;
    Tplot(i) = Tc / (1+(g_amma-1)*Mach^2/2);
    Mplot(i) = Mach;
    plot(Aplot, Tplot)
end

close(h) 

Regards,

Adam

Adam Lewis
Thank you for sharing your experiences. I have made some tests and I can confirm your point that the time is spent mostly in updating the waitbar when running a short loop.In my case, however, I want the user to be able to cancel an operation and see its progress, as he himself can set the length of the loop indirectly. That is why I apply the waitbar.Your test loop runs fine, but I am still experiencing problems with my case.
Ingo
Is this the complete code? If its possible can you add what 'counterend' is set to? Also I do not see where 'waitbarcounter' is defined. I would assume that they are equal, can you confirm that?
Adam Lewis

related questions