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

 채택된 답변

Matt Fig
Matt Fig 2011년 3월 28일

15 개 추천

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.

추가 답변 (3개)

Sean de Wolski
Sean de Wolski 2011년 3월 28일

3 개 추천

doc dlmwrite
doc fwrite

댓글 수: 2

Bill Tubbs
Bill Tubbs 2019년 7월 13일
dlmwrite is not recommended. Use writematrix instead.
Walter Roberson
Walter Roberson 2019년 7월 13일
writematrix() did not exist in 2011 when the answer was posted.

댓글을 달려면 로그인하십시오.

Walter Roberson
Walter Roberson 2017년 12월 16일

1 개 추천

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

CreLox
CreLox 2018년 3월 22일
편집: CreLox 2018년 3월 22일
Should be:
save('MyMatrix.txt', 'A', '-ascii', '-double', '-tabs')
Jairo C Peralta
Jairo C Peralta 2019년 2월 5일
what is the "double" part for?
Walter Roberson
Walter Roberson 2019년 2월 5일
double precision. Without the -double only about 7 digits are written out, about as much as needed to reproduce single precision numbers.

댓글을 달려면 로그인하십시오.

Anmar Mohammed
Anmar Mohammed 2017년 12월 16일

0 개 추천

thank you very much sir

제품

질문:

2011년 3월 28일

댓글:

2019년 7월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by