title: How to convert Matlab variables to .dat (text) file with headers
body: <p><strong>EDITED QUESTION:</strong> </p>
  
  <p>I have 2500 rows x 100 columns data in variable named avg_data_models. I also have 2500 rows x 100 columns variable 'X' and similar size matrix variable 'Y', both containing the co-ordinates. I want to save the values of this variable in a text (.dat) file which must have 302 header lines in the following manner:</p>
  
  <pre><code>avg_data_models
  300
  X_1
  X_2
  .
  .
  .
  X_100
  Y_1
  Y_2
  .
  .
  .
  Y_100
  avg_data_models_1
  avg_data_models_2
  avg_data_models_3
  .
  .
  .
  .
  .
  avg_data_models_100
  </code></pre>
  
  <p>In the above header style, the first line is the name of the file, the 2nd line tells the number of columns (each column has 2500 rows), and the rest of the 300 lines represent the model of each variable respectively - Like 100 models of X, 100 models of Y and 100 models of avg_data_models.</p>