MATLAB: How to save a matrix as text file

1file iofprintfMATLAB

I want to save a matrix as text file.
Each column should be separated by tab.
The output file should be read with any text editor
When the output is opened, it should display the numbers
in the same way it looks like in Matlab.
Thank you for your help
Emerson

Best Answer

One way is to use FPRINTF.
A = round(rand(6,7)*9) % Write this to file.
fid = fopen('Mymatrix.txt','wt');
for ii = 1:size(A,1)
fprintf(fid,'%g\t',A(ii,:));
fprintf(fid,'\n');
end
fclose(fid)
EDIT
Changed the 'w+' to 'wt' in the FOPEN call.
If you have floating point numbers, you may want to use '%20.18f \t' instead of '%g\t' or similar. See FPRINTF for format specifiers.