MATLAB: How to compare words from two text files and get output as number of matching words

diff()match

I am trying to read two text files and get the number of matching words between two files.

Best Answer

% Read the files into strings:
StrA = fileread(FileA);
StrB = fileread(FileB);
% Replace all characters, which are neither letter or the hyphen by *:
StrA(~isstrprop(StrA, 'alpha') & StrA ~= '-') = '*';
StrB(~isstrprop(StrB, 'alpha') & StrB ~= '-') = '*';
% Split the strings to words:
WordA = strsplit(StrA, '*');
WordB = strsplit(StrB, '*');
% Get the common words:
[WordAB, iA, iB] = intersect(WordA, WordB);
Perhaps you do not want to consider the case?
[~, iA, iB] = intersect(lower(WordA), lower(WordB));
WordAB = WordA(iA);
Related Question