How can I change symbolic displays to decimal displays?
Show older comments
How do I display a symbolic expression as a decimal?
6 Comments
Image Analyst
on 20 May 2013
Please give examples. Do you mean like it's showing 1/4 and you want it to show 0.250000, or showing 5/2 and you want it so show 2.50000?
Maria Coronell
on 20 May 2013
Danae Chipoco Haro
on 29 Nov 2016
you can try to add "format short" of "format long" to your code before the number you want to show. Hope that will help you!
Walter Roberson
on 29 Nov 2016
"format short" and "format long" have no effect on symbolic numbers or symbolic expressions.
Kantaphon Meechart
on 21 Apr 2017
Hi, I have the same problem too. I try format short but the answer still fraction number.
Walter Roberson
on 21 Apr 2017
Kantaphon, see my Answer below.
Accepted Answer
More Answers (4)
Shashank Prasanna
on 20 May 2013
>> format
Resets MATLAB display format to default.
2 Comments
Shima Khatiri
on 13 Sep 2017
It's not a solution to this problem.
Steven Lord
on 13 Sep 2017
That will help if you want to change how numeric data is displayed. But it will not change how symbolic expressions are displayed. Walter's answer (with the additional point I added) is a solution and I have just marked it Accepted.
Andrew Gibbons
on 16 Feb 2021
3 votes
Did you try vpa? vpa does a better job of displaying easily readable answers for problems that involve matrices (not 1x1 scalars).
vpa(S,4), where S is what you were trying to solve for (in your case the reynold's or nusselt number) so you would put something like vpa(Nu2,4) to get 4 digits of precision. I hope that helps.
Youssef Khmou
on 20 May 2013
hi, Maria,
If you want to display decimal ( floating point) numbers try :
>>format long % or format short
If you want fractional display try :
>>format rat
and try :
>>doc format
3 Comments
Maria Coronell
on 20 May 2013
Walter Roberson
on 20 May 2013
If you just enter a number at the command line, how does it come out? For example if you entered
2.345
then does it come out in decimal or does it come out in fraction form?
I am trying to determine here whether you MATLAB is set to display fractions automatically or if instead the program is designed to display as fractions deliberately.
Maria Coronell
on 21 May 2013
Felipe Jiménez Hernández
on 22 Jun 2024
Recent versions of Matlab's symbolic toolbox seem to have changed this behavior for the worse, at least in my machines (I'm using 9.9.0.1495850 (R2020b) Update 1 in Windows, don't want to update because the new editor is pure sh*t).
Anyway, apparently you cannot easily see a variable number of digits on screen any more.
I have found a workaround that works up to 16 or 17 digits (but no more, because it relies on conversion to a double). If x is a sym, you can do this:
num2str(eval(char(x))), 16)
This works the same if x=sym('pi') or x=vpa('pi',40). Look:
>> format long g
>> x = sym('pi')
x =
pi
>> vpa(x, 40) % expected to see 40 digits, but no
ans =
pi
>> num2str(x, 40) % num2str does not work on sym variables
Error using num2str (line 47)
Input to num2str must be numeric.
>> num2str(eval(char(x)), 40) % digits from 17 going are wrong for pi (they are of double(pi))
ans =
'3.141592653589793115997963468544185161591'
>> num2str(eval(char(x)), 16) % these are correct for true pi
ans =
'3.141592653589793'
>> x = vpa('pi', 40) % vpa won't fix anything
x =
pi
>> num2str(x, 40) % a vpa number is still a sym
Error using num2str (line 47)
Input to num2str must be numeric.
>> num2str(eval(char(x)), 40) % still wrong from digit 17 going
ans =
'3.141592653589793115997963468544185161591'
If someone knows or finds a way for Matlab to show 40 digits of true pi on screen (whatever precision it uses internally), please say, I will try it.
(By the way, GNU Octave's symbolic package does this the way it should be done. Try it.)
5 Comments
"apparently you cannot easily see a variable number of digits on screen any more."
Of course you can:
n = sym(2)
x = sqrt(n)
vpa(x,40)
However, your example calls SYM with the text 'pi', which you incorrectly think refers to the mathematical constant pi = 3.14159265359.... but in fact since version R2020a, calling SYM('pi') simply creates a symbolic variable using the greek letter π (exactly like it will also create variables using any other greek letter). This change is clearly documented:

"If someone knows or finds a way for Matlab to show 40 digits of true pi on screen (whatever precision it uses internally), please say, I will try it."
The SYM() documentation shows many examples of how to define pi (the mathematical constant) symbolically. The examples are very easy to find for anyone who reads the documentation. The examples all define pi using this syntax (note: the input is the double pi, not text):
x = sym(pi)
vpa(x,40)
Your approach using text input, CHAR() and EVAL() is very indirect, inefficient, relies on undocumented features, and is best avoided.
(By the way, MATLAB's symbolic package clearly documents the way it should be done. Try reading it.)
Felipe Jiménez Hernández
on 22 Jun 2024
Of course? Don't assume what others see in their versions of Matlab. Mine is not even too old.
Look:
>> version
ans =
'9.9.0.1495850 (R2020b) Update 1'
>> n = sym(2)
n =
2
>> x = sqrt(n)
x =
1.4142
>> vpa(x, 40)
ans =
1.4142
So no, I cannot.
Also (your example):
>> version
ans =
'9.9.0.1495850 (R2020b) Update 1'
>> x = sym(pi)
x =
3.1416
>> vpa(x, 40)
ans =
3.1416
And btw, creating the symbolic variable for number pi with the syntax
x = sym(pi)
instead of
x = sym('pi')
is a terrible new idea (a new terrible idea?), because pi in Matlab is double(pi), so you are passing sym a double, and sym has to figure out that you mean the infinitely many digits of pi instead of the finite-precision approximation of pi. ¿When is the double near enough? ¿How do you create a symbolic variable for a finite-precision value of pi? Look (in a newer version, which I refuse to use in my other computers due to the terrible, terrible editor):
>> version
ans =
'9.13.0.2126072 (R2022b) Update 3'
>> format long g
>> pi
ans =
3.14159265358979
>> x = sym(pi)
x =
pi
>> vpa(x, 40)
ans =
3.141592653589793238462643383279502884197
>> x = vpa(3.14159265358979)
x =
3.1415926535897932384626433832795
>> vpa(x, 40)
ans =
3.141592653589793238462643383279502884197
>> x = vpa(3.1415926535898)
x =
3.1415926535897932384626433832795
>> vpa(x, 40)
ans =
3.141592653589793238462643383279502884197
>> x = vpa(3.1415926535897)
x =
3.1415926535896998572638949553948
>> vpa(x, 40)
ans =
3.14159265358969985726389495539478957653
Matlab should learn how things are done in Octave, for example.
Walter Roberson
on 23 Jun 2024
Felipe Jiménez Hernández
on 23 Jun 2024
This is the behavior in my machine, in case you care:
>> ver symbolic
-----------------------------------------------------------------------------------------------------
MATLAB Version: 9.9.0.1495850 (R2020b) Update 1
MATLAB License Number: 40825541
Operating System: Microsoft Windows 11 Home Version 10.0 (Build 22631)
Java Version: Java 1.8.0_202-b08 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
-----------------------------------------------------------------------------------------------------
Symbolic Math Toolbox Version 8.6 (R2020b)
>> format long % just in case
>> sympref('FloatingPointOutput', false);
>> x = sym(pi)
x =
pi
>> x - pi
ans =
0
>> x = sym('pi')
x =
pi
>> x - pi
ans =
pi - pi
>> sympref('FloatingPointOutput', true);
>> x = sym(pi)
x =
3.1416
>> x - pi
ans =
0
>> x = sym('pi')
x =
pi
>> x - pi
ans =
pi - 3.1416
The option does make show some digits on screen, but I cannot see as many as doing my discouraged workaround:
num2str(eval(char(x)), 17)
If there is something less discouraged to show more digits, I will gladly use it, especially if it can show more than 17 digits of true pi. In any case, I understand you prolly don't have my version of the symbolic toolbox to do any tests. (On another front, what would be great would be the option to reverse to the old Matlab editor instead of the new s*tty one --I would update my symbolic toolbox.)
And I also don't know how to define a sym variable for the exact value of pi as opposed to a sym variable representing a numerical value of pi with many correct digits. I just preferred sym('pi') to generate the former rather than a valueless sym variable named pi (pun intended).
"How do you create a symbolic variable for a finite-precision value of pi?"
The SYM() documentation states that you can use the 'f' (for floating point) flag:
sympref('FloatingPointOutput', false);
y = sym(pi,'f')
vpa(y,40)
fprintf('%.39f',pi) % for comparison
"If there is something less discouraged to show more digits, I will gladly use it, especially if it can show more than 17 digits of true pi.... And I also don't know how to define a sym variable for the exact value of pi as opposed to a sym variable representing a numerical value of pi with many correct digits."
The SYM() documentation states that you can use the default 'r' flag, exactly as I showed in my last comment:
x = sym(pi)
vpa(x,40)
Categories
Find more on Special Values 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!