Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a waitbar to work in Matlab?

Tags:

matlab

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.

like image 973
Ingo Avatar asked Aug 26 '10 13:08

Ingo


2 Answers

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

like image 66
Adam Lewis Avatar answered Sep 29 '22 23:09

Adam Lewis


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.

like image 21
Jonas Avatar answered Sep 29 '22 23:09

Jonas