% function to read in a file and count frequency of adjacent letter pairs
function matrix2=pair_freq(filename)
% read in file
fileID=fopen(filename);
input_text=fread(fileID)';
fclose(fileID);
plain_text=simplify_text(input_text);
% make matrix of pairs
matrix=zeros(27,27);
for k=1:length(plain_text)-1
letter1=plain_text(k);
letter2=plain_text(k+1);
matrix(letter1,letter2)=matrix(letter1,letter2)+1;
end % for k
total=sum(sum(matrix));
matrix2=matrix/total;
%return

% display the pairs matrix
depth=max(max(matrix));
figure;
hold on;
title(['Frequency of pairs in ',filename]);
image(matrix)
setc=jet(depth);
colormap(setc)
colorbar;
matrix=matrix/total;
letters={'A','B','C','D','E','F','G','H','I','J','K','L',...
'M','N','O','P','Q','R', 'S','T','U','V','W','X','Y','Z','_'};
set(gca, 'XTickLabel',letters, 'XTick',1:length(letters),...
'YTickLabel',letters, 'YTick',1:length(letters));
return