Unique Words in Multiple Files and Their Frequencies

2 Ansichten (letzte 30 Tage)
Mazhar Iqbal Rana
Mazhar Iqbal Rana am 5 Jan. 2014
Kommentiert: Walter Roberson am 12 Aug. 2017
I Have calculated unique words and their frequencies in a single file below..
fid = fopen(filename);
words = textscan(fid, '%s');
status = fclose(fid);
unique_words = unique(words{1,1});
frequencies = zeros(numel(unique_words), 1);
for i = 1:numel(unique_words)
if max(unique_words{i} ~= ' ')
for j = 1:numel(words{1,1})
if strcmp(words{1,1}(j), unique_words{i})
frequencies(i) = frequencies(i) + 1;
end
end
end
end
Can anyone please tell me that how can I do this for multiple files? I mean if I am having four files? And moreover, after I have list of unique words in single file, how can I check through Matlab code that which words appears how many times in each file?
Thanks

Antworten (1)

Zubier  Abdullah
Zubier Abdullah am 12 Aug. 2017
Bearbeitet: Walter Roberson am 12 Aug. 2017
So you would use something like this
files = dir('*.TXT')
N = numel(files)
count = 0;
for i = 1:length(files)
fid1 = files(i).name
disp(fid1)
fidI = fopen(files(i).name,'r');
end
this will let you open each of the files in the folder (the .Txt means only open text files) and it sends that name to the Fid1)
add this to your code and it should work
  1 Kommentar
Walter Roberson
Walter Roberson am 12 Aug. 2017
files = dir('*.TXT')
N = numel(files)
count = 0;
words = cell(N, 1);
for i = 1:length(files)
fidname = files(i).name
disp(fidname)
fid = fopen(fidname, 'r');
words{N} = textscan(fid, '%s');
fclose(fid);
end
Or, more simply,
files = dir('*.TXT')
filenames = {files.name};
words = arrayfun( @(name) regexp( fileread(name), '\w+', 'split'), filenames, 'uniform', 0);

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Characters and Strings finden Sie in Help 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