Can you use the 'format long' command in the 'fprintf' command?

19 views (last 30 days)
I would like to have variable 'x' formated using the 'format long' command when it is printed using fprintf. However, I am unable to place it between the % and f values of ('%f',x) since 'format long' begins with an f. I have tried using '.15' between % and f, however, the output values are not aligned when the numbers infront of the decimal place increase (see below). Can 'format long' be used in fprintf? If not, how can I keep the numbers in front of the decimal place the same length (in this example, '08' for the last three entries)?
sqrt of a (x) error(e) iteration(s)
32.500000000000000 9.923e+02 1
17.234615384615385 2.330e+02 2
10.474036101145005 4.571e+01 3
8.292191785986859 4.760e+00 4
8.005147977880979 8.239e-02 5
8.000001655289593 2.648e-05 6

Accepted Answer

Stephen23
Stephen23 on 6 Sep 2020
Edited: Stephen23 on 6 Sep 2020
"Can 'format long' be used in fprintf?"
No.
"If not, how can I keep the numbers in front of the decimal place the same length..."
You can align numbers by specifying the field length:
>> M = [32.5,9.923e+02,1;17.234615384615385,2.330e+02,2;10.474036101145005,4.571e+01,3;8.292191785986859,4.760e+00,4;8.005147977880979,8.239e-02,5;8.000001655289593,2.648e-05,6];
>> fprintf('%18.15f %.3e %d\n',M.') % default: pad with spaces
32.500000000000000 9.923e+02 1
17.234615384615385 2.330e+02 2
10.474036101145005 4.571e+01 3
8.292191785986859 4.760e+00 4
8.005147977880979 8.239e-02 5
8.000001655289593 2.648e-05 6
>> fprintf('%018.15f %.3e %d\n',M.') % optional: pad with zeros
32.500000000000000 9.923e+02 1
17.234615384615385 2.330e+02 2
10.474036101145005 4.571e+01 3
08.292191785986859 4.760e+00 4
08.005147977880979 8.239e-02 5
08.000001655289593 2.648e-05 6

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!