Format number in fprintf

조회 수: 5 (최근 30일)
Trang Hu Jia
Trang Hu Jia 2020년 10월 27일
댓글: Sudhakar Shinde 2020년 10월 27일
Hello everyone i have using function of fprintf to create .dat file as
1 0.000000 0.000000 0.000000
2 0.000000 0.000000 4.000000
3 0.000000 0.000000 1.000000
4 0.000000 0.000000 2.000000
5 0.000000 11.000000 4.000000
6 0.000000 12.000000 4.000000
7 0.000000 13.000000 4.000000
8 0.000000 14.000000 4.000000
9 0.000000 15.000000 4.000000
10 0.000000 16.000000 4.000000
11 0.000000 17.000000 4.000000
12 0.000000 18.000000 4.000000
13 0.000000 12.000000 0.000000
14 0.000000 11.000000 0.000000
15 0.000000 10.000000 0.000000 etc.
Is it possible? to re-arrange in the same format as
1 0.000000 0.000000 0.000000
2 0.000000 0.000000 4.000000
3 0.000000 0.000000 1.000000
4 0.000000 0.000000 2.000000
5 0.000000 11.000000 4.000000
6 0.000000 12.000000 4.000000
7 0.000000 13.000000 4.000000
8 0.000000 14.000000 4.000000
9 0.000000 15.000000 4.000000
10 0.000000 16.000000 4.000000
11 0.000000 17.000000 4.000000
12 0.000000 18.000000 4.000000
13 0.000000 12.000000 0.000000
14 0.000000 11.000000 0.000000
15 0.000000 10.000000 0.000000
  댓글 수: 3
Trang Hu Jia
Trang Hu Jia 2020년 10월 27일
Thank you for your replying this topic.
I'm just re-correct the format that I want.
Sudhakar Shinde
Sudhakar Shinde 2020년 10월 27일
check formatspec with fprintf.

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

채택된 답변

Stephen23
Stephen23 2020년 10월 27일
I am guessing that unlike your example you actually want the columns to be aligned, e.g. where M is your matrix:
>> fprintf(' %9d %9.6f %15.6f %15.6f\n',M.')
1 0.000000 0.000000 0.000000
2 0.000000 0.000000 4.000000
3 0.000000 0.000000 1.000000
4 0.000000 0.000000 2.000000
5 0.000000 11.000000 4.000000
6 0.000000 12.000000 4.000000
7 0.000000 13.000000 4.000000
8 0.000000 14.000000 4.000000
9 0.000000 15.000000 4.000000
10 0.000000 16.000000 4.000000
11 0.000000 17.000000 4.000000
12 0.000000 18.000000 4.000000
13 0.000000 12.000000 0.000000
14 0.000000 11.000000 0.000000
15 0.000000 10.000000 0.000000
  댓글 수: 1
Trang Hu Jia
Trang Hu Jia 2020년 10월 27일
This is exactly what I want Stephen, you made my day ! Thank you very much.
Whould you mind to guide me, if I want to change my array to be this one
1 0.000000 0.000000 0.000000
2 0.000000 0.000000 4.000000
3 0.000000 0.000000 1.000000
4 0.000000 0.000000 2.000000
5 0.000000 11.00000 4.000000
6 0.000000 12.00000 4.000000
7 0.000000 13.00000 4.000000
8 0.000000 14.00000 4.000000
9 0.000000 15.00000 4.000000
10 0.000000 16.00000 4.000000
11 0.000000 17.00000 4.000000
12 0.000000 18.00000 4.000000
13 0.000000 12.00000 0.000000
14 0.000000 11.00000 0.000000
15 0.000000 10.00000 0.000000
Thanks in advance.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by