Asked by Anton Gribovskiy
on 22 Sep 2019 at 2:19

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'

Answer by Walter Roberson
on 23 Sep 2019 at 6:12

Accepted Answer

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.

Anton Gribovskiy
on 24 Sep 2019 at 20:27

Thank you. containedDisplay if exactly what I was looking for.

Sign in to comment.

Answer by Bruno Luong
on 22 Sep 2019 at 18:21

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

Anton Gribovskiy
on 22 Sep 2019 at 22:28

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 7 Comments

## Walter Roberson (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/481521-format-number-in-the-same-format-as-disp#comment_748593

## Anton Gribovskiy (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/481521-format-number-in-the-same-format-as-disp#comment_748605

## Walter Roberson (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/481521-format-number-in-the-same-format-as-disp#comment_748737

## Anton Gribovskiy (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/481521-format-number-in-the-same-format-as-disp#comment_748785

## Walter Roberson (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/481521-format-number-in-the-same-format-as-disp#comment_748820

## Anton Gribovskiy (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/481521-format-number-in-the-same-format-as-disp#comment_748824

## Walter Roberson (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/481521-format-number-in-the-same-format-as-disp#comment_748830

Sign in to comment.