'strcmp' works when trying to count number of times a character occurs in a textfile but 'findstr' fails!

2 views (last 30 days)
The purpose is to find the number of times a particular character/word occurs in a multi-line text document. This code works:
function charnum=char_counter(fname,character)
charnum=0;
fid=fopen(fname,"rt");%opens file
if fid<0 || ~ischar(character) %check if file not found or character is not a char
charnum=-1;
return
end
oneline=fgets(fid);
while ischar(oneline)
for x=1:length(oneline)
%compaa=re whether 'character' and words from line are same
if strcmp(oneline(x),character)==true
charnum=charnum+1;
end
end
oneline=fgets(fid);
end
But when I am using strfind, it returns an empty array for each line? strfind works for single line sentences. why does it fail here?
  4 Comments
dpb
dpb on 21 Jun 2020
Well, the problem isn't strfind, it does what it's supposed to do.
The problem is in the test
...
s=strfind(oneread,character);%looking for word in each sentence
if isempty(s==0)%checking if s has any elements
c=c+1;
end
...
if there is a match of the character wanted, strfind will return an array of those locations in the string -- your test of s==0 will always return False -- 0 is the one value strfind canNOT return -- if there's no match it returns [], but [] is NOT the same thing as a 0 at all.
Think about what you have as the result when the comparison succeeds; you don't need but one builtin function on that return variable to directly return the answer you're looking for (since it's homework, we don't just hand out solutions, we provide guiding hints).
You will need a test for the empty case to return a 0 for it, but it's also somewhat more simple code than you've written as well (hint).
The other thing your function is missing is it does not close the file handle when the EOF condition is reached so you'll have dangling file handles after every time you run the function. At your command window type in
fclose all
to clean up your present environment.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 21 Jun 2020
s=strfind(oneread,character);%looking for word in each sentence
strfind() returns a list of indices in oneread where the pattern character starts. If no locations are found then strfind() returns empty.
if isempty(s==0)%checking if s has any elements
s itself can be empty, but it will never contain any 0. In the context of your code, s==0 will be an array of false values the same size as s. Checking isempty() of that would be the same as checking isempty(s)
c=c+1;
So you are counting the number of times that no matches were found in the line.
Perhaps you intended
if isempty(s)==0 %checking if s has any elements
which would be the same as
if ~isempty(s)
in which case you would at least be counting the number of lines that it was found on.
But you are supposed to be checking the count of matches, not the number of lines: if the pattern occurs more than once on the same line, then it should be counted more than once.
If you are intended to count words, then you need to be careful. Suppose you are asked to count 'the' and the text is 'The theoretical theatre theologizes thews." Then the proper answer is either zero ('The' is not 'the') or one (if case distinctions are to be ignored), but you would find four.

More Answers (1)

Image Analyst
Image Analyst on 20 Jun 2020
This homework question was asked before - before we knew it was homework I think.
Anyway, see the answer here:

Categories

Find more on Characters and Strings 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!