Unique Words in Multiple Files and Their Frequencies

3 visualizaciones (últimos 30 días)
Mazhar Iqbal Rana
Mazhar Iqbal Rana el 5 de En. de 2014
Comentada: Walter Roberson el 12 de Ag. de 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

Respuestas (1)

Zubier  Abdullah
Zubier Abdullah el 12 de Ag. de 2017
Editada: Walter Roberson el 12 de Ag. de 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 comentario
Walter Roberson
Walter Roberson el 12 de Ag. de 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);

Iniciar sesión para comentar.

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by