MATLAB: How to display numbers in long format using fprintf

format longfprintffprintf formatMATLAB

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?

Best Answer

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.)