MATLAB (and Fortran) store matrices in a column-first ordering, while in your file, you want to save numbers in a row-first order. The fprintf command passes the numbers of S in based on linear indexing into S:
>> S
S =
3.8795 0 0
0 1.4585 0
0 0 0.3144
0 0 0
>> fprintf([repmat('%8.4f\t',1,size(S,2)) '\n'],S)
3.8795 0.0000 0.0000
0.0000 0.0000 1.4585
0.0000 0.0000 0.0000
0.0000 0.3144 0.0000
Passing the transpose of S in fixes the issue:
>> fprintf([repmat('%8.4f\t',1,size(S,2)) '\n'],S')
3.8795 0.0000 0.0000
0.0000 1.4585 0.0000
0.0000 0.0000 0.3144
0.0000 0.0000 0.0000
You can also do this more directly using the writematrix command:
>> writematrix(S, 'S.txt')
>> type S.txt
3.87951723431537,0,0
0,1.4585169755725,0
0,0,0.31437233188791
0,0,0