How make MATLAB waitbar message indicate the filename being processed?

Hi All
I have to process some files in a folder in a for loop.
as per examples in web , I wanted to try :
h = waitbar(0,'Please wait...');
for step = 1:1000
% computations take place here
waitbar(step/1000)
drawnow
end
close(h)
but this, has one problem. I want the window message change with the change in filename and say like : processing filename1. etc.
the other issue is : this progress bar flashes in each loop, and closes immediately till the next iteration. there is no control over that ?
what does drawnow do ?

2 Comments

Have you read the documentation pages for waitbar and drawnow?
sorry, yes, but it was too complicated and I had never used it before

Sign in to comment.

 Accepted Answer

persistent h
if isempty(h) || ~isvalid(h)
h = waitbar(0,'Please wait...');
end
for step = 1:1000
thisfilename = FileNames{step}; %adjust this line as needed
waitbar(step/1000, h, sprintf('Processing %s', thisfilename))
% computations take place here
drawnow
end
waitbar(1, h, 'Processing done. Proceeding with other computations')
This will update the waitbar message each iteration. It will also keep the waitbar alive between invocations of the code, such as if you have another loop around all of this.

14 Comments

dear Walter , it did not work, it gives me this error , meanwhile the window flashes only , same as before
Error: Output argument "yout" (and maybe others) not assigned during call to "step".
Here is an example of how the function step works:
Consider a randomly generated stable Transfer Function Model:
of the form G(s)=num(s)/den(s):
num =
1.1437 1.2753 1.5756 0.3862
den =
1.0000 6.4067 16.8741 9.9803
Call step using the following command (see also, help step):
step(tf(num,den));
You would get that error if you tried to use either of the two lines
thisfilename = FileNames{step}; %adjust this line as needed
or
waitbar(step/1000, h, sprintf('Processing %s', thisfilename))
before the line
for step = 1:1000
had been executed.
Thank you very much ! my lack of attention !
ok now another error for the last line :
Error using waitbar (line 92)
The second argument must be a message character vector or a handle to an existing waitbar.
it seems it doesn't like the 3 parameters in
waitbar(1, h, 'Processing done. Proceeding with other computations')
You did not notice that I removed the call to close(h) .
I did not put close(h) either, but how is it related to the error to the above line ? I don't understand
I think I did not get you, could you please help me a bit more ? I tried with and without close(h) but did not work
Example code:
%sample data to have *something* to cycle through
filenames = cellstr( char(randi([0 9], 1000, 5)+'A') );
for K = 1 : 3
do_files(filenames);
end
function do_files(FileNames)
persistent h
if isempty(h) || ~isvalid(h)
h = waitbar(0,'Please wait...');
end
nfiles = length(FileNames);
for step = 1:nfiles
thisfilename = FileNames{step}; %adjust this line as needed
waitbar(step/nfiles, h, sprintf('Processing %s', thisfilename))
% computations take place here
drawnow
end
waitbar(1, h, 'Processing done. Proceeding with other computations');
end
I tested this.
Nowhere in the code you posted are you using the variable h1, so this error is impossible with this code.
Another Confess !!! I also have clear(vars2clear{:}) in the try catch !! I think this is definitely the problem but I did not put h in it to be cleared. Now I have disabled clear all and I passed from the error :
Reference to a cleared variable h.
to :
The second argument must be a message character vector or a handle to an existing waitbar.
please check the structure bellow : I hope I could spot the error
persistent h
if isempty(h) || ~isvalid(h)
h = waitbar(0,'Please wait...');
end
if condition
clear all
%filename definition here
for f1=1:filenumber
try
clear(vars2clear{:})
waitbar(f1/numel(Flist), h, sprintf('Processing %s', filename))
%computation
drawnow
end
end
waitbar(1, h, 'Processing done. Proceeding with other computations');
end % this end is for the if condition since I have other elseifs
Why are you clearing variables at all? And clear all is even worse.
And a suggestion to keep the readability of this thread: feel free to edit your comments if you have additional comments. The 5 comments above could probably be merged to a single coherent comment.
ok, the necessity of clearing variables is not to accumulate values when iterating the same computations on multiple files. But as I said: I disabled clear all, and I get the second error I mentioned. and by now, you can just concentrate on the last code I have written above.I have also dropped the phrase catch after try, but it is there in my code. only forgot to put it here. the genral structureof try catch is respected. I should add that this h variable has become blue, like a nested function, but , in my above structure that I mentioned, is there any nested function? how to resolve this ?
the necessity of clearing variables is not to accumulate values when iterating the same computations on multiple files
That would not be an issue if you were using functions, or if you were initializing the variables at the appropriate place.
now, you can just concentrate on the last code I have written above
Using "clear all" is like using dynamite to blow up the bridge you are standing on, with you hoping that the dynamite will destroy the very concept of "space" in the area below you and that therefore you will not fall.
If you are using "clear all" in a program, neither Rik nor I will be able to predict how your program will work, and also you should not expect that the behavior of a program that uses "clear all" is repeatable.
If you must have "clear all" in some program, then restrict it to being part of one "reset MATLAB" script that you run by hand. There is no need for having more than one "clear all" call in the entire set of MATLAB programs that you will ever write in your programming career.
problem solved. had to paste h = waitbar(0,'Please wait...'); also into the try loop
No, the code
persistent h
if isempty(h) || ~isvalid(h)
h = waitbar(0,'Please wait...');
end
takes care of that. As I showed in
that code is within the section that is being invoked repeatedly.

Sign in to comment.

More Answers (0)

Categories

Find more on App Building in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!