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

1 次查看(过去 30 天)
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

采纳的回答

Walter Roberson
Walter Roberson 2011-3-28
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 个评论
Emerson De Souza
Emerson De Souza 2011-3-28
Thank you Walter,
I used the first expression and
it made what I needed.
I wish you a nice day
Emerson

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by