for example i want to find the word " lege" in the document and then to count how mnay times apear this specific word.in a text like that :
How I can find a specific word in a notepad text?
2 views (last 30 days)
Show older comments
I have a notepad text and i want to find a specific word in this and then to count how many times do i found him .
Accepted Answer
dpb
on 6 Apr 2023
Edited: dpb
on 6 Apr 2023
"i want to find the word " lege" in the document and then to count how mnay times apear this specific word"
txt=readlines('yourfile.txt'); % return file as string array
MyWord="lege"; % define the lookup string
nTimes=count(txt,MyWord,'IgnoreCase',true); % find and count
It doesn't appear from the many posts you've done a lot of digging/reading in the doc nor looked at the examples on analyzing text...start with <TopLevelCharacter&Strings> and dig down. In particular to get an overview of facilities very quickly click on the 'Functions' link at the top and peruse through it for features.
NOTA BENE AND ADDENDUM:
The above will find/count the string "lege" anywhere in the document and will match the string "LEGEA ..." as well as "PRIN LEGE NU" that also occurs in the sample text (btw, if you really want help, attach the actual text as text, not as an image thereof so folks can do something with it besides just look at it).
The Q? isn't totally clear as to what kind of match you're looking for; you also say to find "this specific word" which could be interpreted to be only the latter of the two above examples is wanted. If that were to be the case, then
wordpat=whitespacePattern+MyWord+whitespacePattern;
nTimes=count(txt,wordpat,'IgnoreCase',true);
will find only the cases where the wanted string is separated by whitespace. It wouldn't find it if it were the last word in a sentence or had some other punctuation directly before/behind it, though.
2 Comments
dpb
on 6 Apr 2023
Edited: dpb
on 6 Apr 2023
So, did you try the sample code??? -- and what's the answer to the Q? regarding the search type/match wanted?
fn='https://www.mathworks.com/matlabcentral/answers/uploaded_files/1347944/proba.txt';
txt=readlines(fn);
txt(1:3)
MyWord="lege";
nTimesA=count(txt,MyWord,'IgnoreCase',true)
NA=sum(nTimesA)
wordpat=whitespacePattern+MyWord+whitespacePattern;
nTimesB=count(txt,wordpat,'IgnoreCase',1)
NB=sum(nTimesB)
Other than correcting a couple missepllings, looks like the suggestion would works just fine...undoubtedly some more thoughtful search patterns would be useful to find specific items of interest -- like the first "(LEGEA..." is found by the first search but not the second because of the leading "(" instead of being separated by whitespace. That's easily-enough fixed, but left as "exercise for student".
More Answers (1)
Vilém Frynta
on 6 Apr 2023
I have created and attached a file with Lorem Ipsum text as an example.
Now, I will try to show you how to find word 'ipsum' and it's counts in the file.
% Read the file
str = fileread('ipsum.txt');
% Put all the words into the cell
wrds = strsplit(str, ' ')
% Find the 'ipsum' words
idx = strfind(wrds, 'ipsum');
idx = find(not(cellfun('isempty', idx)));
% Find the occurences
N = length(idx)
0 Comments
See Also
Categories
Find more on Matrix Indexing 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!