Errors in looping/erros checking

조회 수: 1 (최근 30일)
NUR KHAIRUNNISA rahimi
NUR KHAIRUNNISA rahimi 2011년 11월 20일
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')

채택된 답변

Alex
Alex 2011년 11월 20일
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
Alex
Alex 2011년 11월 21일
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 2011년 11월 21일
Also, I don't understand what isn't working correctly.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2011년 11월 20일
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
NUR KHAIRUNNISA rahimi
NUR KHAIRUNNISA rahimi 2011년 11월 21일
tried that before, but nothing seemed to work
Jan
Jan 2011년 11월 21일
Instead of "nothing seemed to work" a description of the occurring errors/problems would be more helpful.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by