フィルターのクリア

How to count number of words in a text file?

4 ビュー (過去 30 日間)
Sandy
Sandy 2016 年 8 月 10 日
コメント済み: Mustafa Abd 2019 年 4 月 5 日
I have a text file with a large number of words, 'filename.txt'.
I have a column of words 'A'.
Example:
A =
HJK.BT
KLP.OK
POL.ST
I want to go through column A and count how many times each of the words in column A appears in the text file. How can I do this? I tried 'textscan' but have had no luck.
Basically, I want to end up with something like:
Column A # of Times Occurred
HJK.BT 3
KLP.OK 4
POL.ST 2

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 8 月 10 日
A={'HJK.BT'
'KLP.OK'
'HJK.BT'
'POL.ST'
'HJK.BT'
'POL.ST'}
[ii,jj,kk]=unique(A)
freq=accumarray(kk,1)
out=[ii num2cell(freq)]
  6 件のコメント
Sandy
Sandy 2016 年 8 月 10 日
Yes! Thank you so much!
Azzi Abdelmalek
Azzi Abdelmalek 2016 年 8 月 10 日
A = {'GS.JYG.BHZ';'GS.TSS.BHZ'}
fid=fopen('fi.txt')
str=textscan(fid,'%s')
fclose(fid)
text=str{:}
for k=1:numel(A)
freq(k)=sum(ismember(text,A{k}))
end

サインインしてコメントする。

その他の回答 (1 件)

Sarah Palfreyman
Sarah Palfreyman 2018 年 4 月 30 日
See extractFileText in Text Analytics Toolbox
  1 件のコメント
Mustafa Abd
Mustafa Abd 2019 年 4 月 5 日
how can me get this toolbox?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeText Data Preparation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by