URGENT!: Code that will open a text file (paragraph), delete all non-alphabetical characters, then sort through to find how often a word occurs and orders based on occurrence, then alphabetically. Output will then be printed to a text tile.

1 view (last 30 days)
This is the code I have come up with so far. This prints the words and occurence, but does not let me print it in occurence order, then alphabetical. I was wondering if anybody knew how to do this. I want the final format to be (but with the words in the file):
word: word count: 12
word: word2 count: 11
word: yell count: 11
--------------------------------------------------------------------------
fid = fopen('filename.txt');
if fid == -1
fprintf('Opening of the file was not successful. \n')
else
s = textscan(fid, '%s');
fclose(fid);
end
str = strjoin(lower(s{1}));
str = erase(str,'.');
str = erase(str,'!');
str = erase(str,'?');
str = erase(str,',');
str = erase(str,';');
str = erase(str,':');
str = erase(str,'-');
Sp = cell(1);
Sp{1} = strsplit(str);
uniquestr = unique(Sp{1})';
for k = 1:numel(uniquestr)
freq(k) = sum(strcmp(Sp{1},uniquestr(k)));
end
freq = num2cell(freq)';
wrdct = [uniquestr freq]

Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!