# Matlab Help



## sneezekoofer (Oct 30, 2010)

Hi, I'm having a bit of trouble formatting my output.

I want to display:

D1 = 1.0000 + 7.0000x + 31.2100(x-2)^2
D2 = ... etc.

and I want it to be such that we don't know how many D's there will be. So I have a loop set up 

syms x;
k =1;
while k<=n-1
S = y(k) + b(k)*(x-z(k)) + c(k)*(x-z(k))^2 + d(k)*(x-z(k))^3;
disp(sprintf('S%d=',k))
disp(S);
k = k+1;
end

but I don't know how to make it show 4 decimal places, even if my number is an integer. It won't let me use sprintf, because x is syms. How can setprecision it to 4?

Also, there's another place in my program that I want it to say:
x = 0.0000 1.0000 45.0000
.. etc.

but sprintf forces be to skip a space, so instead I did: 

disp(sprintf('\nx:')) 
disp(z) 
disp(sprintf('\ny:'))
disp 

but again, I don't know how to force my z array to have 4 digits, and how to stop my sprintf from skipping a line after "x:" and "y:".

Please help!


----------



## reventon (Oct 16, 2009)

Hi,



> Also, there's another place in my program that I want it to say:
> x = 0.0000 1.0000 45.0000
> .. etc.
> 
> ...


For this one - equivalent code to the one you got there.


```
[FONT=Lucida Console]fprintf('x = ')
fprintf('%0.4f ',x)
fprintf('\n')
fprintf('y = ')
fprintf('%0.4f ',y)
fprintf('\n')[/FONT]
```
There is probably a neater way to do it - but that should do what you require. Change the "4" in *%0.4f* to change the number of decimal places.

I am unsure of what you are trying to achieve with the first one. Would it be possible to upload the script file or is that sensitive data?

Regards


----------



## sneezekoofer (Oct 30, 2010)

Okay great, thanks!

For the first part of my question, I just meant, is there any way to make my output always be 4 digits? Because when I use fprintf and sprintf I get an error:

??? Error using ==> fprintf
Function 'fprintf' is not defined for values of class 'sym'.
Error in ==> C:\MATLAB6p5\work\ppart2.m
On line 98 ==> fprintf('%0.4f',S)

so in order to output my S which has a symbol, x, I used disp(S); which works fine, but there are no decimal points. 

I know in c++ what I'm looking for is something like setprecision(4)<<fixed ... or something like that, so I was just wondering if a similiar thing exists for Matlab?

(Using fprint will give me output 
S1 = 0.0000 + 0.7500(x - 0.0000) + 0.0000(x - 0.0000)^2 + 0.2500(x - 0.0000)^3
S2 = 1.0000 + 1.5000(x - 1.0000) + 0.7500(x - 1.0000)^2 + -0.2500(x - 1.0000)^3

which isn't too bad, but outputting S directly would get rid of the unnecessary terms.)


----------



## reventon (Oct 16, 2009)

sneezekoofer said:


> (Using fprint will give me output
> S1 = 0.0000 + 0.7500(x - 0.0000) + 0.0000(x - 0.0000)^2 + 0.2500(x - 0.0000)^3
> S2 = 1.0000 + 1.5000(x - 1.0000) + 0.7500(x - 1.0000)^2 + -0.2500(x - 1.0000)^3
> 
> which isn't too bad, but outputting S directly would get rid of the unnecessary terms.)


Sorry, I don't know MATLAB that well, just had to use it a couple of times.

Where did you put the *fprintf* statements to achieve the above output?


----------

