I suppose I'll make this an answer instead of just a comment, since nobody has answered yet.
If all you wanted is to construct the diagonal matrix of eigenvalues, then if the matrix has a complete set of eigenvalues (i.e., it is not a defective matrix) then yes, you will have created the matrix D.
Interestingly, what you did was partly of no use. For example, the call to flipud does not do as much as you think. I assume you wanted the eigenvalues in some special sequence, but roots does not guarantee any special order for the result. They are usually ROUGHLY in sequence. For example...
-0.89835 + 0.90816i
-0.89835 - 0.90816i
0.55269 + 0.84217i
0.55269 - 0.84217i
-0.13261 + 0i
You can see they were initially sorted so the roots with largest magnitude come out first. But that need not always happen. Higher degree polynomials may find that sequence is only approximately sorted in magnitude.
Is your matrix always symmetric? Perfectly so? If not, then expect complex eigenvalues are probable. But you can use sort instead of flipud to gain a more accurately sorted set of eigenvalues.
But note that what you are doing does NOT give you a set of eigenvectors, thus the matrix S.
Anyway, a fine point is that as you want to construct the eigenvalues, IF your goal is for some strange reason NOT to use eig? That reason fails miserably here. Why? Because you are STILL going to use eig in the end. The point is, you seem to want to use roots to avoid using eig. But you STILL use eig, because roots calls eig! That is, roots converts the polyynomial coefficient into a matrix, then calls eig on that matrix.
So honestly, it seems silly. You take a matrix, convert it into a characteristic polynomial, then call roots. But roots then converts that polynomial BACK into a matrix. Then calls eig. Do you see the problem here? Why bother? Your subterfuge does nothing, except waste CPU cycles. MATLAB still calls eig in the end.
Finally, it is a bad idea to name your variable char, because at some point, you will find the function char is a useful one, and having a variable with that name can cause your code to fail with strange looking bugs.