If I understood your input data correct, your loop is equivalent to:
fprintf(fid,[repmat('%.16g ',1,size(S,1)) '|\n'],permute(real(S),[1,3,2]));
fprintf
starts over using the format string when it reaches the end. The permute is necessary to preserve the order from your code.