MATLAB: ??? Undefined function or method 'spri

2019-09-04 16:53发布

I´m trying to show 16 decimal place of a result. The code I put is this

clear x;
x = 0.245;
1-x+1/2*x.^2-1/6*x.^3+1/24*x.^4
sprint('%0.16f', ans)

Matlab give me this answer

ans =

0.7827

??? Undefined function or method 'sprint' for input arguments of type 'char'.

I have two question:

  1. What happen? I think I used it before and I had no problems with 'sprintf' for show a result with several decimal places.
  2. What can I do to show more decimal places?

Thank you!

3条回答
不美不萌又怎样
2楼-- · 2019-09-04 17:33

Well, I think 'vpa' this help me to show more decimal places

clear x;
clear expresion;
x = 0.245;
expresion = 1-x+1/2*x.^2-1/6*x.^3+1/24*x.^4
%sprint('%0.16f', ans)
vpa(expresion,16)

EDIT: and this is the matlab answer:

expresion =

0.7827


ans =

.7827116041927082
查看更多
女痞
3楼-- · 2019-09-04 17:41

I think you did not use sprint before. There is no MATLAB intrinsic function called sprint, you ought to use sprintf.

查看更多
甜甜的少女心
4楼-- · 2019-09-04 17:42

sprintf formats data into a string; it does not display it for output. Furthermore, it's sprintf, not sprint, which is the function you've typed- and that MATLAB is complaining about. (It doesn't know what sprint is, but it knows about sprintf.)

If you mean to save ans to a string as a number to 16 decimal places, use sprintf. To just display it, which I think is what you want, use printf instead. In either case, the issue is clear; you forgot the f in sprintf!

查看更多
登录 后发表回答