- 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.
Format number in the same format as disp
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
Walter Roberson
2019-9-23
I should have specified "under format long g as that is the format we were talking about.
采纳的回答
Walter Roberson
2019-9-23
matlab.internal.display.containedDisplay(value,width)
formats value according to the current format, provided that the formatted result would be that width or less. If the formatted result would be longer, it returns the empty string ""
There is a lower-level routine matlab.internal.display.containedDisplayHelper that can also accept the format specification to use such as 'longG'; it needs its input packaged a particular way though.
As of R2019b, the internal display routines appear to be:
matlab.internal.display.commandWindowWidth
matlab.internal.display.containedDisplay
matlab.internal.display.containedDisplayHelper
matlab.internal.display.dimensionString
matlab.internal.display.format
matlab.internal.display.formatSpacing
matlab.internal.display.getCellDisplayOutput
matlab.internal.display.getContainedClassName
matlab.internal.display.getDimensionSpecifier
matlab.internal.display.getHeader
matlab.internal.display.getNewlineCharacter
matlab.internal.display.getObjectHeaderHelper
matlab.internal.display.isDesktopInUse
matlab.internal.display.isHot
matlab.internal.display.language
matlab.internal.display.numericDisplay
matlab.internal.display.numericDisplayHelper
matlab.internal.display.printWrapped
matlab.internal.display.truncateLine
matlab.internal.display.wrappedLength
Some of those have .m source in toolbox/matlab/lang/+matlab/+internal/+display but most are built-in.
更多回答(1 个)
Bruno Luong
2019-9-22
>> 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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!