MATLAB: ??? Undefined function or method 'spri

2019-09-04 16:56发布

问题:

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!

回答1:

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!



回答2:

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:

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