How to display numbers in long format using fprintf?

35 views (last 30 days)
I changed the format of the script file from the default to long, and when I type a variable name into the command window it outputs in long format, but not when I try and output it using fprintf. x is a column vector that I want the results of in long format, each output in the same line as their variable names.
format long
fprintf('X1 = %f', x(1))
This outputs
X1 = 1.598803
If I just typed 'x(1)' into the command window, the output is 1.598802934802056, which is the number of decimal points that I want. I know I can just hard code it to have 15 decimal points in the fprintf line, but why doesn't it just accept the format I specified earlier?

Accepted Answer

Walter Roberson
Walter Roberson on 10 Feb 2019
fprintf() and sprintf() ignore whatever format is in effect.
The only format specification that exactly matches one of the fprintf() / sprintf() format specifications, is format bank, which corresponds to %.2f .
When you examine the fine details, really it is format that is inconsistent. The behavior of fprintf() and sprintf() follows POSIX standards (except for not giving precise enough results on MS Windows.)

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!