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

 Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 10 Aug. 2016

1 Stimme

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 Kommentare

Sandy
Sandy am 10 Aug. 2016
But how can I see how many times the words in Column A occur in the text file?
Azzi Abdelmalek
Azzi Abdelmalek am 10 Aug. 2016
Can you post a sample of your text file?
A={'HJK.BT'
'KLP.OK'
'POL.ST'}
text='HJK.BT dsdlsd sd KLP.OK ddd HJK.BT4 ss 14c KLP.OK ddsfsf f ff POL.ST HJK.BT'
for k=1:numel(A)
freq(k)=numel(regexp(text,A{k}))
end
Sandy
Sandy am 10 Aug. 2016
Bearbeitet: Sandy am 10 Aug. 2016
I couldn't post it, it was marked as spam but basically it's a very large text file with multiple columns of data. I want to search how many times each word in Column A occurs in the WHOLE text file.
Column A is NOT part of the text file, it's separate.
Sandy
Sandy am 10 Aug. 2016
Yes! Thank you so much!
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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Sarah Palfreyman
Sarah Palfreyman am 30 Apr. 2018

0 Stimmen

See extractFileText in Text Analytics Toolbox

Kategorien

Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by