Progress bar

7 visualizzazioni (ultimi 30 giorni)
NUR KHAIRUNNISA rahimi
NUR KHAIRUNNISA rahimi il 24 Nov 2011
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.

Risposte (3)

Naz
Naz il 24 Nov 2011
put
fprintf('%d\n',progBar);
inside the loop

Jan
Jan il 24 Nov 2011
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 Commenti
NUR KHAIRUNNISA rahimi
NUR KHAIRUNNISA rahimi il 24 Nov 2011
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 il 24 Nov 2011
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.

Accedi per commentare.


Sameer Kumthekar
Sameer Kumthekar il 14 Dic 2011
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

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by