How to write txt file contain double array and text header?

조회 수: 4 (최근 30일)
Danupon Subanapong
Danupon Subanapong 2019년 3월 23일
댓글: Danupon Subanapong 2019년 3월 23일
Hello!!!!
I have to write a text file containg array and text header. My numeric array is in size of 33731x48. I knew that I can use fprint to output text file, but my double array has too many columns. I don't know how to set up the formatting operator for all double array element. I want all element to be floating point number (%f). I am thinking about writing 48 %f inside fprintf function For example,
fprintf(fid, '%f %f %f... %f r\n', array);
and there are 48 %f inside fprintf function, but it should be better way. So, could you please help me giving some advice?
  댓글 수: 2
madhan ravi
madhan ravi 2019년 3월 23일
Could you upload your numeric matrix as .mat file?
Danupon Subanapong
Danupon Subanapong 2019년 3월 23일
Hello Ravi,
Thank you so much for your reply. I attached part of numeric array. I cannot upload all data since it exceeds 5MB. But I think it is fine. Please check it.

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

채택된 답변

Rik
Rik 2019년 3월 23일
편집: Rik 2019년 3월 23일
You can use repmat or sprintf to construct the FormatSpec.
array=rand(3000,48);
fid=fopen('test.txt','wt');%opening with the t flag auto-converts \n to \r\n on Windows
fprintf(fid,'some cool header text\n');
FormatSpec=[repmat('%f ',1,size(array,2)) 'r\n'];%or should that have been \r\n instead?
fprintf(fid,FormatSpec, array);
fclose(fid);
  댓글 수: 3
Rik
Rik 2019년 3월 23일
Sorry, that was a typo. The repmat function call now creates a 2D array, instead of a vector, I'll edit my code to fix this.
Danupon Subanapong
Danupon Subanapong 2019년 3월 23일
Thank you so much!! It really helps.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by