function S = readScoreMatrix(matrixFileName)

%READSCOREMATRIX Reads a scoring matrix from a file
% S = readScoreMatrix(matrixFileName) reads a scoring matrix
% from a file. The score matrix must contain NxN entries,
% and must be symmetric.

% Read entries from the file containing the matrix
fid = fopen(matrixFileName, 'r');
L = fgetl(fid);
i = 0;
M = [];
while (isstr(L)) % Read until L is not empty
i = i+1;
M(i,:) = sscanf(L, '%f', [1, inf]);
L = fgetl(fid);
end

% Ensure that the scoring matrix is symmetric
epsilon = 0.001; % Round-off error
if (size(M,1) ~= size(M,2) | norm(triu(M)-tril(M)') > epsilon)
error('The scoring matrix must be symmetric');
end

S = M;