How to save a matrix as text file?

91 visualizaciones (últimos 30 días)
Emerson De Souza
Emerson De Souza el 28 de Mzo. de 2011
Comentada: Walter Roberson el 13 de Jul. de 2019
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

Respuesta aceptada

Matt Fig
Matt Fig el 28 de Mzo. de 2011
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.

Más respuestas (3)

Sean de Wolski
Sean de Wolski el 28 de Mzo. de 2011
doc dlmwrite
doc fwrite
  2 comentarios
Bill Tubbs
Bill Tubbs el 13 de Jul. de 2019
dlmwrite is not recommended. Use writematrix instead.
Walter Roberson
Walter Roberson el 13 de Jul. de 2019
writematrix() did not exist in 2011 when the answer was posted.

Iniciar sesión para comentar.


Walter Roberson
Walter Roberson el 16 de Dic. de 2017
You could also consider dlmwrite telling it to use \t as the delimiter.
You could also consider using
save('MyMatrix.txt', 'A', '-double', '-tab')
Where A is the name of the variable
  3 comentarios
Jairo C Peralta
Jairo C Peralta el 5 de Feb. de 2019
what is the "double" part for?
Walter Roberson
Walter Roberson el 5 de Feb. de 2019
double precision. Without the -double only about 7 digits are written out, about as much as needed to reproduce single precision numbers.

Iniciar sesión para comentar.


Anmar Mohammed
Anmar Mohammed el 16 de Dic. de 2017
thank you very much sir

Etiquetas

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by