Format number in fprintf

5 vues (au cours des 30 derniers jours)
Trang Hu Jia
Trang Hu Jia le 27 Oct 2020
Commenté : Sudhakar Shinde le 27 Oct 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 commentaires
Trang Hu Jia
Trang Hu Jia le 27 Oct 2020
Thank you for your replying this topic.
I'm just re-correct the format that I want.
Sudhakar Shinde
Sudhakar Shinde le 27 Oct 2020
check formatspec with fprintf.

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 27 Oct 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 commentaire
Trang Hu Jia
Trang Hu Jia le 27 Oct 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Entering Commands dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by