Progress bar

I have a list of words(maybe consist of 250000) where I would like to remove the plural form of all the word ending with s. While I am doing that, I have to dispplay a progress bar on the screen.
So this is what I have so far. However,
for iword=1:length(wordbank)
if ~isempty(wordbank{iword})&& strcmp(wordbank{iword}(end), 's') no_plurals=no_plurals+1; wordbank2{iword} = wordbank{iword}(1:(end-1));
progBar=(iword/(length(wordbank)))*100;
end
fprintf('%d\n',progBar);
however, my program only show the end result of the process, which is 100% and not incrementing the value while processing from 1% to 100%. Can you help me fix this? Thank you.

回答 (3 件)

Naz
Naz 2011 年 11 月 24 日

0 投票

put
fprintf('%d\n',progBar);
inside the loop
Jan
Jan 2011 年 11 月 24 日

0 投票

Cleaned a little bit:
fprintf(' ');
fmt = [char([8,8,8,8]), '%3d%%'];
singular = 0; % Nicer than no_plural
nword = length(wordbank);
wordbank2 = cell(1, nword);
update = 1;
for iword = 1:nword
word = wordbank{iword};
if ~isempty(word) && word(end) == 's'
singular = singular + 1;
wordbank2{iword} = word(1:(end-1));
progBar = 100 * iword / nword;
if progBar > update
fprintf(fmt, update);
drawnow;
update = round(progBar) + 1;
end
end
end
EDITED: overwrite the percentage string
But there are much faster methods. E.g. see FEX: strncmpr.
index = strncmpr(wordbank, 's', 1);
wordbank2 = wordbank(~index);
The timings (Matlab 2009a, Win7, 250'000 words with 6 ot 7 characters):
Loop with progress display: 2.0 sec
Loop without progress display: 0.8 sec
strncmpr: 0.02 sec
Are you sure you want to waste the most time for updating the progress display?

4 件のコメント

NUR KHAIRUNNISA rahimi
NUR KHAIRUNNISA rahimi 2011 年 11 月 24 日
so I have tried this, and this what came out
loading the word % 4
loading the word % 4
loading the word % 4
loading the word % 16
loading the word % 16
loading the word % 16
loading the word % 28
loading the word % 28
loading the word % 36
loading the word % 40
loading the word % 40
loading the word % 40
loading the word % 52
loading the word % 52
loading the word % 52
loading the word % 64
loading the word % 68
loading the word % 68
loading the word % 68
loading the word % 80
loading the word % 80
loading the word % 88
loading the word % 92
loading the word % 96
loading the word % 96
the statement doubles at certain point am I am not sue why that happen. Actually the way I want to do it is not to print each number out, its more like
loading the word % 4
and then it increases to
loading the word % 16 or incrementing in the same line, and not exactly printing the same line over and over again.
Jan
Jan 2011 年 11 月 24 日
If you copy and paste my program, you do not get this output. Therefore I cannot know, how your out is created.
NUR KHAIRUNNISA rahimi
NUR KHAIRUNNISA rahimi 2011 年 11 月 24 日
I know it will take more time to do the progress display but it is a requirement in the program I am writing. I have no choice but to include it.
Alright, I tried to include these lines in my code according to my program
progBar = 100 * iword / nword;
if progBar > update
fprintf('%d %%\n', update);
drawnow;
update = round(progBar) + 1;
end
and this what came out
5 %
LOAD WORD BANK
Loading word bank: none....started
Loading word bank: sample_bank_small_awords.
Successfully loaded 25 words from the word bank file
Removing invalid words..4 words were successfully removed.
Removing duplicate words and sorting...done
Removed 2 duplicate words
Removed 2 plural word
Building word indices and calculating beginning letter counts...done
Calculating word length counts...done
Final word count:23
Press ENTER
LOAD WORD BANK
Loading word bank: none....started
Loading word bank: sample_bank_small_awords.
Successfully loaded 25 words from the word bank file
Removing invalid words..4 words were successfully removed.
Removing duplicate words and sorting...done
Removed 2 duplicate words
Removed 2 plural word
Building word indices and calculating beginning letter counts...done
Calculating word length counts...done
Final word count:23
Press ENTER
LOAD WORD BANK
Loading word bank: none....started
Loading word bank: sample_bank_small_awords.
Successfully loaded 25 words from the word bank file
Removing invalid words..4 words were successfully removed.
Removing duplicate words and sorting...done
Removed 2 duplicate words
Removed 2 plural word
Building word indices and calculating beginning letter counts...done
Calculating word length counts...done
Final word count:23
Press ENTER
17 %
LOAD WORD BANK
Loading word bank: none....started
Loading word bank: sample_bank_small_awords.
Successfully loaded 25 words from the word bank file
Removing invalid words..4 words were successfully removed.
Removing duplicate words and sorting...done
Removed 3 duplicate words
Removed 3 plural word
Building word indices and calculating beginning letter counts...done
Calculating word length counts...done
Final word count:22
Press ENTER
from what I can see, it is not doing what I'd like it to do though.
Jan
Jan 2011 年 11 月 24 日
I've inserted some code to overwrite the percentage string inplace.
I do not know, why your program is not doing, what you expect. But I do neither know the program nor what you expect. Perhaps you want to explain both? But as far as I can see, the original question is answered.

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

Sameer Kumthekar
Sameer Kumthekar 2011 年 12 月 14 日

0 投票

Hi , I think you can use waitbar something like this..
h = waitbar(0,'Removing the plural form of all the words...Please wait..!');
steps = 100; for step = 1:steps code here h = waitbar(step/steps,sprintf('%d%%',step)); end close(h);
Please check once! Sameer K

カテゴリ

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

タグ

質問済み:

2011 年 11 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by