How make MATLAB waitbar message indicate the filename being processed?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
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 ?
Accepted Answer
Walter Roberson
on 4 Apr 2020
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
farzad
on 4 Apr 2020
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));
Walter Roberson
on 4 Apr 2020
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.
farzad
on 4 Apr 2020
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')
Walter Roberson
on 4 Apr 2020
You did not notice that I removed the call to close(h) .
farzad
on 4 Apr 2020
I did not put close(h) either, but how is it related to the error to the above line ? I don't understand
farzad
on 4 Apr 2020
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
Walter Roberson
on 4 Apr 2020
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.
Rik
on 5 Apr 2020
Nowhere in the code you posted are you using the variable h1, so this error is impossible with this code.
farzad
on 5 Apr 2020
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
Rik
on 5 Apr 2020
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 ?
Walter Roberson
on 5 Apr 2020
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.
farzad
on 5 Apr 2020
problem solved. had to paste h = waitbar(0,'Please wait...'); also into the try loop
Walter Roberson
on 5 Apr 2020
Edited: Walter Roberson
on 5 Apr 2020
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.
More Answers (0)
Categories
Find more on App Building in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)