Format number in the same format as disp
Show older comments
I want to format number to the string with the same format as used by disp to output numbers. So I want to write function my_format that would take number and output same string as disp. I don't need new lines, just correctly formatted number. For example
>> format
>> pi
ans =
3.1416
>> my_format(pi)
ans =
'3.1416'
>> format long
>> pi
ans =
3.141592653589793
>> my_format(pi)
ans =
'3.141592653589793'
7 Comments
Walter Roberson
on 22 Sep 2019
Examine the output of
for F = [-inf,-101:-99,-5:15, 99:101,inf]; disp(F);disp(-pi*10^(F));end
under the format that you want to use.
Notes:
- format short, format short g, format long, format long g -- none of those correspond exactly to a single sprintf() format, not even when only processing a single number.
- the output of disp() for a vector or array or multidimensional array can be quite different than the output for a scalar and different than the output of disp() for a different 2D slice of a multidimensional array
- you need undocumented internal calls to find out which format is currently in effect: matlab.internal.display.format()
- variables within a table are not displayed with exactly the same format as is used for disp(). Instead, an sprintf() format is used that does not exactly agree with what format would choose
- The exact output of disp() depends upon the operating system you are running on.
Because of the above factors, I recommend against this course of action. I would instead recommend that you use sprintf() with a fixed format (perhaps chosen considering the data type)
Anton Gribovskiy
on 22 Sep 2019
Walter Roberson
on 22 Sep 2019
I guess for tables matlab uses matlab.mixin.CustomDisplay with their custom implementation of getPropertyGroups.
No, it does not. It uses loops and num2str() in toolbox/matlab/datatypes/@tabular/disp.m
May be you have any leads on documentation of format styles?
There is no documentation of exactly what the output format is for each case.
I did not expect that disp is different on different platforms
disp() uses C or C++ level library calls that eventually get resolved by operating system libraries. The different operating systems format numbers slightly differently. The most obvious symptom is the number of digits after an exponent, with some platforms using two digits and other platforms using three digits. There are also some differences in how rounding is done (MS Windows having a history of truncating instead of rounding properly).
Also, historically if you requested 16 digits after the decimal point or more with fprintf() or sprintf() or num2str() then MS Windows just output zeros, and Linux output more digits but became inaccurate, and Mac was able to give a perfect representation of the internal binary. (This is what led to the File Exchange contribution num2strexact) This does not affect disp() as disp() does not attempt to output the 16th digit in any of the formats, but it does show you how tied everything is to OS-level routines.
The exact representation from the MS Windows routines changed a couple of years ago; I think it was Windows 10 that finally improved, but I am not at all certain of that.
But single sprintf does not produce desirable output when input can have any range.
What range of values has an undesirable output if you use a %g format of suitable width? Could you give an example of an input that has a different output representation between format long g and fprintf() with a %.15g format and for which you find the %.15g output undesirable ?
Anton Gribovskiy
on 22 Sep 2019
Walter Roberson
on 23 Sep 2019
disp([0.1 0.2]) will produce the same output as fprintf('%26.15g%26.15g\n', [0.1 0.2])
Anton Gribovskiy
on 23 Sep 2019
Walter Roberson
on 23 Sep 2019
I should have specified "under format long g as that is the format we were talking about.
Accepted Answer
More Answers (1)
Bruno Luong
on 22 Sep 2019
>> x=logspace(1,3,10)
x =
1.0e+03 *
0.0100 0.0167 0.0278 0.0464 0.0774 0.1292 0.2154 0.3594 0.5995 1.0000
>> disp(x) % same as above
1.0e+03 *
0.0100 0.0167 0.0278 0.0464 0.0774 0.1292 0.2154 0.3594 0.5995 1.0000
>> str=evalc('disp(x)');
>> fprintf('\nx =\n\n%s', str)
x =
1.0e+03 *
0.0100 0.0167 0.0278 0.0464 0.0774 0.1292 0.2154 0.3594 0.5995 1.0000
1 Comment
Anton Gribovskiy
on 22 Sep 2019
Categories
Find more on Characters and Strings 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!