function matrix2file(M,nbrFormat,fname)
% M is a 2D array of numbers
% Creates .dat file with name specified by the string fname.
% The ith line in the file is M(i,:) displayed with the format
% specified by the string nbrFormat

[nr,nc] = size(M);
fid = fopen([fname '.dat'],'w');
for i=1:nr
str = sprintf(nbrFormat,M(i,:));
fprintf(fid,'%s\n',str);
end
fclose(fid);