Unique Words in Multiple Files and Their Frequencies

1 view (last 30 days)
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

Answers (1)

Zubier  Abdullah
Zubier Abdullah on 12 Aug 2017
Edited: Walter Roberson on 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 Comment
Walter Roberson
Walter Roberson on 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);

Sign in to comment.

Categories

Find more on Characters and Strings 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!