MATLAB assigment Write a function (words.m) that takes as input the filenames of
ID: 3574508 • Letter: M
Question
MATLAB assigment
Write a function (words.m) that takes as input the filenames of an input text file and output text file and writes the list of unique words found in the input text file to the output text file with one word per line. The words should be case-insensitive and punctuation should be ignored (but spaces should be preserved, obviously). Use the isletter and isspace functions. If you use cell arrays, remember that they use {} for indexing.
Example: input.txt Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo is a grammatically correct sentence in American English. >> words('input.txt','output.txt');
output.txt (does not need to be sorted) a american buffalo correct english grammatically in is sentence
Explanation / Answer
data = textread('input.txt', '%s', 'delimiter', '');
a = strsplit(lower(data{1}), ' ');
u = unique(a);
str1 = 'a american ';
str2 = strcat(str1,u);
str3 = ' correct english grammatically in is sentence';
str = strcat(str2,str3);
fid = fopen('output.txt','wt');
fprintf(fid,'%s', str);
fclose(fid);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.