How to save a matrix as text file? PROBLEM REFORMULATION!!!

1 visualización (últimos 30 días)
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.
For example, let the matrix be
M=
1 4 7
2 5 8
3 6 9
I tried to save using the following command:
save MATRIX.txt M -ASCII -tabs
but when I opened either with Notepad,
I got the following as a result:
MATRIX.txt=
1.0000000e+000 4.0000000e+000 7.0000000e+000
2.0000000e+000 5.0000000e+000 8.0000000e+000
3.0000000e+000 6.0000000e+000 9.0000000e+000
How to correct this command or to write a new command
such that input and output are identical?
Thank you for your help
Emerson

Respuesta aceptada

Walter Roberson
Walter Roberson el 28 de Mzo. de 2011
fid = fopen('Matrix.txt', 'wt');
fprintf(fid, [repmat('%g\t', 1, size(M,2)-1) '%g\n'], M.');
fclose(fid)
There is a different approach:
fid = fopen('Matrix.txt', 'wt');
fwrite(fid, '%s', evalc('disp(M)'));
fclose(fid)
This will literally output whatever Matlab would, including things like writing a vector in a cell array out as (e.g.) [2 x 3 double]
  1 comentario
Emerson De Souza
Emerson De Souza el 28 de Mzo. de 2011
Thank you Walter,
I used the first expression and
it made what I needed.
I wish you a nice day
Emerson

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by