The problem is that MATLAB stores data in column-major order, meaning that when you do s1(:)
, the first three values are the first three values in the first column not the first row. (This is how fprintf
will read values out of s1
.) For example:
>> M = magic(3)
M =
8 1 6
3 5 7
4 9 2
>> M(:)
ans =
8
3
4
1
5
9
6
7
2
You can simply transpose the matrix to output the way you want:
fprintf(fid, '%f %f %f\n', s1.');