title: char function in fprintf matlab: keeping new lines when writing to file
body: <p>I am trying to pass a <code>char()</code> function's output to <code>fprintf</code> so that I can write multiple lines to a text file. The problem is that <code>fprintf(fid,'%s\n', str)</code> is neglecting all the blank spaces between the lines, and prints one large line instead. I'd like to print the <code>str</code>, without the loss of the new lines.</p>
  
  <pre><code>str = (char(textlinepre));
  filename = 'newfile.dat';
  fid = fopen(filename,'w');
  fprintf(fid,'%s\n', str);    
  fclose(fid);
  </code></pre>
  
  <p>Original dat/text file content, which is read in as a cell array of strings to the variable <code>textlinepre</code>.</p>
  
  <pre><code>%Created with Spacar 2.0.25 at 18-05-17, 11:43:37
  
  % leafspring1
  BEAM        1   1    2   3    4   0.0000000000   1.0000000000   0.0000000000
  % solid bar
  BEAM        2   3    4   5    6   0.0000000000   0.0000000000   1.0000000000
  % leafspring2
  BEAM        3   5    6   7    8   0.0000000000   1.0000000000   0.0000000000
  
     X        1   0.0000000000   0.0000000000   0.0000000000
     X        3   0.0000000000   0.1000000000   0.1000000000
     X        5   0.0000000000   0.0000000000   0.1000000000
     X        7   0.0000000000   0.1000000000   0.0000000000
  
  FIX 1 1
  FIX 1 2
  FIX 1 3
  FIX 2 2
  FIX 2 3
  FIX 2 4
  FIX 7 1
  FIX 7 2
  FIX 7 3
  FIX 8 2
  FIX 8 3
  FIX 8 4
  
  RLSE 3 1
  RLSE 3 2
  RLSE 3 3
  RLSE 3 4
  RLSE 3 5
  RLSE 3 6
  
  DYNE 1 1
  DYNE 1 2
  DYNE 1 3
  DYNE 1 4
  DYNE 1 5
  DYNE 1 6
  
  
  END
  HALT
  
  XF 3 0.000000000000 0.000000000000 0.000000000000
  
  EM 1 0.070650000000 0.000001909022 0.000001907550 0.000000001472 0.000000000000
  EM 2 1.999400000000 0.000094804883 0.000066646667 0.000028158217 0.000000000000
  EM 3 0.070650000000 0.000001909022 0.000001907550 0.000000001472 0.000000000000
  
  ESTIFF 1 1890000.000000000000 0.058028906301 51.030000000000 0.039375000000 0.000000000000 0.000000000000
  ESTIFF 3 1890000.000000000000 0.000000000000 0.000000000000 0.039375000000 0.000000000000 0.000000000000
  
  EDAMP 1 0.365400000000 0.000006500000 0.000190000000 0.000005300000
  EDAMP 3 0.365400000000 0.000000000000 0.000000000000 0.000005300000
  
  TIMESTEP        10.00 50
  ITERSTEP 10 10 0.00005
  
  END
  END
  
  VISUALIZATION
  BEAMVIS  1 0.000500000000 0.018000000000
  BEAMVIS  2 0.013000000000 0.020000000000
  BEAMVIS  3 0.000500000000 0.018000000000
  
  VIBRATIONMODE 1
  VIBREND 7.854
  ENLARGEFACTOR 0.01
  TRANSPARENCY 0.6
  </code></pre>