Format Array column in fprintf

4 views (last 30 days)
Trang Hu Jia
Trang Hu Jia on 27 Oct 2020
Edited: Stephen23 on 28 Oct 2020
Hello everyone,
I have used the command fprintf to write the data in .dat file
but the result give me 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 10.000000 16.000000 4.000000
11 10.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.
How to re-format to get
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 10.00000 16.00000 4.000000
11 10.00000 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 etc.

Accepted Answer

Stephen23
Stephen23 on 27 Oct 2020
Edited: Stephen23 on 27 Oct 2020
The short answer is: not easily.
Your output example has varying numbers of fractional digits
0.000000 % six decimal places
4.000000 % six decimal places
11.00000 % five decimal places
and also varying numbers of significant digits:
0.000000 % six significant figures
4.000000 % seven significant figures
11.00000 % seven significant figures
This excludes using one simple fprintf command (i.e. based on %f or %g) using only your input matrix.
The long answer: one possible approach is to create an array with the required number of significant figures (or decimal places) for each value and use this with the '*' placeholder syntax, e.g. where M is your matrix:
>> X = 7-(M==0);
>> Y = reshape(permute(cat(3,X,M),[3,2,1]),8,[]);
>> fprintf(' %9d %#9.*g %#15.*g %#15.*g\n',Y(2:end,:))
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 10.00000 16.00000 4.000000
11 10.00000 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
  3 Comments
Stephen23
Stephen23 on 28 Oct 2020
Edited: Stephen23 on 28 Oct 2020
"How to fixed it ?"
What MATLAB version are you using?
If you are using Octave then note that it counts significant digits incorrectly for zero:
>> fprintf('%#.3g\n',[0,1,9])
0.00
1.00
9.00
in which case you can simply do this:
>> fprintf(' %9d %#9.7g %#15.7g %#15.7g\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.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
Trang Hu Jia
Trang Hu Jia on 28 Oct 2020
I got the result already, thank you very much for your help.
This code is very helpful for me
Thanks again!!

Sign in to comment.

More Answers (0)

Categories

Find more on Entering Commands in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!