Errors in looping/erros checking

4 Ansichten (letzte 30 Tage)
NUR KHAIRUNNISA rahimi
NUR KHAIRUNNISA rahimi am 20 Nov. 2011
I have made a few changes from last time but nothing seemed to work out. The codes that I wrote did not manage to process the textfile when the text file is called. To see whether something is process or not, I used the fprintf statement as an indicator where it'll show the total number of words that was loaded and the result was dissapointing. May someone point out which part of it that does not seem to work?
[FILENAME, pathname] = uigetfile('*.wsb','Read Matlab Code File');
if isequal(FILENAME,0) || isequal(pathname,0)
fprintf('User pressed cancel\n');
else
fprintf('User selected: %s \n', FILENAME);
end
fid = fopen(FILENAME,'r+');
if fid<0
%error could not find the file
return,
end
total_no_words=0;
lineNUM=1;
while ~feof(fid)
tline = fgetl(fid);
if ~isempty(tline)
%line is empty, skip it
total_no_words=total_no_words+1;
if sum(isletter(tline))==length(tline)
%line does not contain character besides letters
%we finally have a string
tline=strtrim(tline);
if sum(isspace(tline))==0
%tline contain no spaces and only contain letters
if length(tline) > 3 && length(tline)<26
if strcmp(tline,lower(tline))==1 || strcmp(tline,upper(tline))==1
wordbank=struct;
letters= 'a':'z'; % a, b, c, ..., z
for ichar = 1:length(letters)
wordbank.(letters(ichar))=cell.empty;
wordbank.(tline(1)){end+1,1} = tline
end
end
end
end
end
end
lineNUM=lineNUM+1;
end
fprintf('LOAD WORD BANK \n');
fprintf('Loading word bank: none....started\n');
fprintf('Loading word bank: %s\b\b\b\n',FILENAME);
fprintf('Successfully loaded %d words from the word bank file\n',total_no_words)
fprintf('Removing invalid words...%d words were successfully removed...\n') %not complete
fprintf('Removing duplicate words and sorting...done\n')
fprintf('Removed %d duplicate words\n')
fprintf('Searching for and removing any plural forms of words ending in S:%%\n')
fprintf('Removed %d plural word\n')
fprintf('Building word indices and calculating beginning letter counts...done\n')
fprintf('Calculating word length counts...done\n')
fprintf('Final word count: %d\n')

Akzeptierte Antwort

Alex
Alex am 20 Nov. 2011
Your code here is counting the number of lines (fgetl), not the number of words.
tline = fgetl(fid);
if ~isempty(tline)
%line is empty, skip it
total_no_words=total_no_words+1;
To get the # of words, one option is to parse the line based on white space delimiter.
word_array = get_tokens(tline,' ');
%returns a cell array with words seperated by spaces
num_words = num+words + length(word_array);
%counts the number of words
  5 Kommentare
Alex
Alex am 21 Nov. 2011
I still don't know exactly what you are trying to do. Can you please be more clear on the goal of this project?
Alex
Alex am 21 Nov. 2011
Also, I don't understand what isn't working correctly.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 20 Nov. 2011
At the very least, change
for ichar = 1:length(letters)
wordbank.(letters(ichar))=cell.empty;
wordbank.(tline(1)){end+1,1} = tline
end
to
for ichar = 1:length(letters)
wordbank.(letters(ichar))=cell.empty;
end
wordbank.(tline(1)){end+1,1} = tline
Your code still won't be right but that change might perhaps get you out of the mental grove you are stuck in.
  2 Kommentare
NUR KHAIRUNNISA rahimi
NUR KHAIRUNNISA rahimi am 21 Nov. 2011
tried that before, but nothing seemed to work
Jan
Jan am 21 Nov. 2011
Instead of "nothing seemed to work" a description of the occurring errors/problems would be more helpful.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by