Unique Words in Multiple Files and Their Frequencies

2 次查看(过去 30 天)
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

回答(1 个)

Zubier  Abdullah
Zubier Abdullah 2017-8-12
编辑:Walter Roberson 2017-8-12
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 个评论
Walter Roberson
Walter Roberson 2017-8-12
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);

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by