Format number in fprintf

3 Ansichten (letzte 30 Tage)
Trang Hu Jia
Trang Hu Jia am 27 Okt. 2020
Kommentiert: Sudhakar Shinde am 27 Okt. 2020
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 Kommentare
Trang Hu Jia
Trang Hu Jia am 27 Okt. 2020
Thank you for your replying this topic.
I'm just re-correct the format that I want.
Sudhakar Shinde
Sudhakar Shinde am 27 Okt. 2020
check formatspec with fprintf.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 27 Okt. 2020
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 Kommentar
Trang Hu Jia
Trang Hu Jia am 27 Okt. 2020
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Graphics Object Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by