i want to count the occurrences of words in a text file.

7 views (last 30 days)
i would like to know how to count how many times a word occurs in a text file and then print each word out with it's occurrence number next to it. this list of words should be organized in order of how many times the word occurs (most frequent at the top), and for words that occur the same number of times, the should be organized alphabetically. i would appreciate any help as i am pretty new to matlab.
  1 Comment
Jan
Jan on 7 Apr 2014
Edited: Jan on 7 Apr 2014
Is this a homework question and do you expect the forum to solve your homework? If you are new to Matlab, trying is the best strategy. You can post what you have tried so far and ask for specific problems.
I would not rely on complete solutions posted here to be the best strategy to learn Matlab.

Sign in to comment.

Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 7 Apr 2014
fid = fopen('file.txt');
s=textscan(fid,'%s')
fclose(fid);
str=s{:}
[ii,jj,kk]=unique(str)
freq=hist(kk,(1:numel(jj))')'
[ii num2cell(freq)]
  4 Comments
renuka
renuka on 18 Oct 2019
I want to count number of similar words in a text file. Any one please give me the matlab code

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!