how to use a simple statement to describe a repeat format output about the function "fprintf"?

조회 수: 24 (최근 30일)
For example, fprintf(fid,'%4.2f %4.2f %4.2f %4.2f %4.2f',data) there are 5 '%4.2f', Can a simple statement like the (5'%4.2') to simplify the output format? I have no idea about it and do not find in the documents of matlab.

채택된 답변

Matt Fig
Matt Fig 2012년 11월 17일
fprintf(fid,repmat('%4.2f',1,5),data)
  댓글 수: 3
Matt Fig
Matt Fig 2012년 11월 22일
편집: Matt Fig 2012년 11월 22일
C = {'%5.2f ';'%4.3f ';'\n'};
[C{[1 2*ones(1,8) 3]}]
You can build C with as many elements as needed then pass in an index to get the order and number you need.
Henry Wolkowicz
Henry Wolkowicz 2025년 4월 19일
편집: Henry Wolkowicz 2025년 4월 19일
can one complicate things with
C={'%5.2f & '} for latex table purposes? I keep getting 'number' echos if I try this

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

추가 답변 (1개)

Image Analyst
Image Analyst 2012년 11월 17일
No need to replicate the format string - it will do that automatically. Try it this way:
fprintf(fid,'%4.2f ', data);
  댓글 수: 3
Image Analyst
Image Analyst 2012년 11월 22일
Not sure I understand. That will have every 9th element print with a field width of 5 and the 8 numbers in between print with a field width of 4. Also the printout line will have 9 numbers per row whereas your array has 8 numbers per row. Is that what you want, or not? Because that's what you'd be telling it to do.
Liang He
Liang He 2012년 11월 22일
Thank you for your answer, the Matt Fig has solved it.

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

카테고리

Help CenterFile Exchange에서 Standard File Formats에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by