필터 지우기
필터 지우기

How can I write a matrix into a tab delimited .dat file with header?

조회 수: 3 (최근 30일)
Hi,
I would like to ask you the following: I have a matrix (lets call it Output[]) with numbers (integ, decimal and bool) and I want to transform it into a tab delimited .dat file in a format that includes a *header *in the first line and also curly brackets in every row like the following:
Number of Output rows: 240 %this number will be actually assigned by the size of the matrix
{ 102 103 3.5 2.7 1 2.3 } %the spaces are actually tabs
{ 1223 305 8 9 3 0.5 }
{ 507 109 6.5 8.7 2 7.3 }
...and so on
Can somebody help me to do it with fprintf?
Thanks, Iro

채택된 답변

Walter Roberson
Walter Roberson 2013년 5월 2일
fid = fopen('YourFile.dat', 'wt');
fprintf(fid, 'Number of Output rows: %d\n', size(Output,1));
fprintf(fid, '{\t%g\t%g\t%g\t%g\t%g\t%g\t}\n', Output.' );
fclose(fid);
Each \t represents a tab.
Notice the .' after Output. This is needed so that the output comes out by rows. (It works. Worry about why another time ;-) )

추가 답변 (1개)

Iro
Iro 2013년 5월 2일
Hi Walter,
thank you for your answer. I understand the transpose to some extent but I do not get the dot. However it indeed worked! :)
What about the 'wt' instead of 'w' in fopen? Would that mean something in case I want to overwrite this file for each iteration of my code?
Thanks again,
Iro
  댓글 수: 1
Walter Roberson
Walter Roberson 2013년 5월 2일
'wt' means you are writing a text file, and is otherwise the same as 'w'.
.' is pure transpose, whereas ' without a dot is conjugate transpose

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

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by