MATLAB: Format number in fprintf

arrayfprintf

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

Best Answer

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