Counting the spesific word in text file
5 views (last 30 days)
Show older comments
Hello, I have a text file and I want to see how much 'Hi there!' word inside of it. How can I do it? Thank you.
2 Comments
Walter Roberson
on 3 Jun 2022
What do you want to have happen if one of the occurrences is "PHi there!"? or "hi there!"?
Accepted Answer
Image Analyst
on 3 Jun 2022
Edited: Image Analyst
on 3 Jun 2022
Did you try
fileChars = fileread(fileName);
locations = strfind(fileChars, 'Hi there!')
numLocations = numel(locations)
1 Comment
Walter Roberson
on 3 Jun 2022
If you have "hi there" with lower case H, and you are searching for "Hi there!' with upper case H and with ! then the count should be 0.
More Answers (2)
VINAYAK LUHA
on 3 Jun 2022
Edited: VINAYAK LUHA
on 3 Jun 2022
data = fileread("content.txt")
pattern="hi there!"
substr_freq =count(data,pattern)
%substr_freq has your answer
See Also
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!