Errors in looping/erros checking
Show older comments
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')
Accepted Answer
More Answers (1)
Walter Roberson
on 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 Comments
NUR KHAIRUNNISA rahimi
on 21 Nov 2011
Jan
on 21 Nov 2011
Instead of "nothing seemed to work" a description of the occurring errors/problems would be more helpful.
Categories
Find more on Text Data Preparation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!