title: How can I write strings and matrices to a .txt file in MATLAB?
body: <p>I need to write data to a .txt file in MATLAB. I know how to write strings (<code>fprintf</code>) <em>or</em> matrices (<code>dlmwrite</code>), but I need something that can do both of them. I'll give an example below:</p>
  
  <pre><code>str = 'This is the matrix: ' ;
  mat1 = [23 46 ; 56 67] ;
  %fName
  if *fid is valid* 
      fprintf(fid, '%s\n', str)
      fclose(fid)
  end
  dlmwrite(fName, *emptymatrix*, '-append', 'delimiter', '\t', 'newline','pc')
  dlmwrite(fName, mat1, '-append', 'newline', 'pc')
  </code></pre>
  
  <p>This works okay, but with a problem. The first line of the file is:</p>
  
  <pre><code>This is the matrix: 23,46
  </code></pre>
  
  <p>Which is not what I want. I want to see:</p>
  
  <pre><code>This is the matrix:
  23 46
  56 67
  </code></pre>
  
  <p>How can I solve this? I can't use a for loop and <code>printf</code> solution as the data is huge and time is an issue.</p>