function [] = MatrixToTextFile(A,filename,formatspec,delimiter)

fileID = fopen(filename,'w');

[m,n] = size(A);
if (min(n,m) > 0)
    for i = 1:m
        for j = 1:n
            if (j < n)
                fprintf(fileID,[formatspec,delimiter],A(i,j));
            else
                fprintf(fileID,formatspec,A(i,j));
            end
        end
        if (i < m)
            fprintf(fileID,'\n');
        end
    end
end

fclose(fileID);

end