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 件のコメント

Rik
Rik 2020 年 4 月 4 日
Have you read the documentation pages for waitbar and drawnow?
farzad
farzad 2020 年 4 月 5 日
sorry, yes, but it was too complicated and I had never used it before

サインインしてコメントする。

 採用された回答

Walter Roberson
Walter Roberson 2020 年 4 月 4 日

0 投票

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 件のコメント

farzad
farzad 2020 年 4 月 4 日
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
Walter Roberson 2020 年 4 月 4 日
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
farzad 2020 年 4 月 4 日
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
Walter Roberson 2020 年 4 月 4 日
You did not notice that I removed the call to close(h) .
farzad
farzad 2020 年 4 月 4 日
I did not put close(h) either, but how is it related to the error to the above line ? I don't understand
farzad
farzad 2020 年 4 月 4 日
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
Walter Roberson 2020 年 4 月 4 日
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
Rik 2020 年 4 月 5 日
Nowhere in the code you posted are you using the variable h1, so this error is impossible with this code.
farzad
farzad 2020 年 4 月 5 日
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
Rik 2020 年 4 月 5 日
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.
farzad
farzad 2020 年 4 月 5 日
編集済み: farzad 2020 年 4 月 5 日
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
Walter Roberson 2020 年 4 月 5 日
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
farzad 2020 年 4 月 5 日
problem solved. had to paste h = waitbar(0,'Please wait...'); also into the try loop
Walter Roberson
Walter Roberson 2020 年 4 月 5 日
編集済み: Walter Roberson 2020 年 4 月 5 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeApp Building についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by