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
0 个评论
回答(1 个)
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
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 Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!