278 views (last 30 days)
Show older comments
Ibrahim A on 22 Mar 2021
-
-
Link
Direct link to this question
https://www.mathworks.com/matlabcentral/answers/780212-how-do-i-convert-fractional-display-to-decimal-display
Commented: Steven Lord on 22 Mar 2021
Accepted Answer: Steven Lord
I am getting bunch of fractional values in my output matrix and I have other matrixes outputting the same way as this one. I cannot find a way to change it to decimal notation, something like 1.678.
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
Accepted Answer
Steven Lord on 22 Mar 2021
Call double or vpa on your symbolic variable.
6 Comments Show 4 older commentsHide 4 older comments
Show 4 older commentsHide 4 older comments
Ibrahim A on 22 Mar 2021
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/780212-how-do-i-convert-fractional-display-to-decimal-display#comment_1410267
⋮
vpa works but double doesn't. I am getting this error. Thank you for your answer.
Steven Lord on 22 Mar 2021
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/780212-how-do-i-convert-fractional-display-to-decimal-display#comment_1410292
It's generally not a good idea to create a variable named double in your code as it prevents you from calling the double function.
Ibrahim A on 22 Mar 2021
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/780212-how-do-i-convert-fractional-display-to-decimal-display#comment_1410302
Open in MATLAB Online
I am not creating a variable named double, I am? I just try to convert symbolic function to double, just using double in front of the integral(int). This works perfectly when I use vpa instead of double. Coming back to your first answer, where should I call the double instead. Something like this ..
syms double(t);
Steven Lord on 22 Mar 2021
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/780212-how-do-i-convert-fractional-display-to-decimal-display#comment_1410372
Open in MATLAB Online
Looking more closely at the line of code where the error occurred I see that there are three possible causes of that error, only one of which I called out. You could also have defined a variable named int, so int(j1, t) is being interpreted as an attempt to index into the variable rather than an attempt to call the int function.
A third option is that t is your symbolic variable. When you assign something symbolic to an expression of the form "identifier(symbolicVariable)" on the left side of the equals sign, you create a symbolic function. Since vpa returns a symbolic expression, vpa will work here. But when you try to assign a number to that same expression, MATLAB tries to figure out to which element of the array whose name is specified the number should be assigned. But MATLAB can't figure out which element is element t and so throws an error.
syms t
f(t) = t*pi % creates a symbolic function, works
f(t)=
f(1:5) % evaluate f
ans=
g(t) = pi % assigns pi to the tth element of g, errors
Error using sym/subsindex (line 864)
Invalid indexing or function definition. Indexing must follow MATLAB indexing. Function arguments must be symbolic variables, and function body must be sym expression.
Ibrahim A on 22 Mar 2021
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/780212-how-do-i-convert-fractional-display-to-decimal-display#comment_1410422
Open in MATLAB Online
I use this function to find the integral of 1, I think this is what you mentioned above. Is this the problem?
j1(t) = (j_max * (t.^0))
qdd(t) = vpa(int(j1,t))
Steven Lord on 22 Mar 2021
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/780212-how-do-i-convert-fractional-display-to-decimal-display#comment_1410437
Open in MATLAB Online
two = sym(2);
sqrt2 = sqrt(two)
sqrt2=
V = vpa(sqrt2)
V=
1.4142135623730950488016887242097
D = double(sqrt2)
D = 1.4142
whos two sqrt2 V D
Name Size Bytes Class Attributes D 1x1 8 double V 1x1 8 sym sqrt2 1x1 8 sym two 1x1 8 sym
V, sqrt2, and two are all sym. So in the code below the line assigning to f(t) creates a symbolic function:
syms t
f(t) = V
f(t)=
2.6651441426902251886502972498731
D is a double. In the code below assigning to g(t) attempts to store D in element t of the array g.
g(t) = D
Error using sym/subsindex (line 864)
Invalid indexing or function definition. Indexing must follow MATLAB indexing. Function arguments must be symbolic variables, and function body must be sym expression.
Compare:
qdd(t) = V % works
qdd(t) = D % errors
Sign in to comment.
More Answers (1)
Bjorn Gustavsson on 22 Mar 2021
Open in MATLAB Online
You might have set format to rat somewhere, perhaps in a startup.m or the like. You could get back to more normal decimal output by something like this:
format short g
For additional options check the help and documentation to format.
HTH
1 Comment Show -1 older commentsHide -1 older comments
Show -1 older commentsHide -1 older comments
Ibrahim A on 22 Mar 2021
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/780212-how-do-i-convert-fractional-display-to-decimal-display#comment_1410147
Unfortunately, didn't work. I also tried to change the format on preferences -> matlab -> command window -> numeric format (Short g, compact), that also didn't work. I should also mention that I am using symbolic math toolbox, I don't know if it is related or not. But thank you for your answer.
Sign in to comment.
Sign in to answer this question.
See Also
Categories
Mathematics and OptimizationSymbolic Math ToolboxMathematicsNumbers and Precision
Find more on Numbers and Precision in Help Center and File Exchange
Tags
- decimal
- fraction
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office