Counting the spesific word in text file

5 vues (au cours des 30 derniers jours)
tinkyminky93
tinkyminky93 le 3 Juin 2022
Modifié(e) : Image Analyst le 3 Juin 2022
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 commentaires
Walter Roberson
Walter Roberson le 3 Juin 2022
What do you want to have happen if one of the occurrences is "PHi there!"? or "hi there!"?
tinkyminky93
tinkyminky93 le 3 Juin 2022
Phi there is not meaningful and it is impossible to found in my text file. But it is a good point.

Connectez-vous pour commenter.

Réponse acceptée

Image Analyst
Image Analyst le 3 Juin 2022
Modifié(e) : Image Analyst le 3 Juin 2022
Did you try
fileChars = fileread(fileName);
locations = strfind(fileChars, 'Hi there!')
numLocations = numel(locations)
  1 commentaire
Walter Roberson
Walter Roberson le 3 Juin 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.

Connectez-vous pour commenter.

Plus de réponses (2)

VINAYAK LUHA
VINAYAK LUHA le 3 Juin 2022
Modifié(e) : VINAYAK LUHA le 3 Juin 2022
data = fileread("content.txt")
pattern="hi there!"
substr_freq =count(data,pattern)
%substr_freq has your answer
  1 commentaire
tinkyminky93
tinkyminky93 le 3 Juin 2022
Modifié(e) : tinkyminky93 le 3 Juin 2022
Search term must be a text or pattern array, error using count

Connectez-vous pour commenter.


tinkyminky93
tinkyminky93 le 3 Juin 2022
Modifié(e) : tinkyminky93 le 3 Juin 2022
you are right sir @Walter Roberson, I got the point.

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by