title: Saving a file in MATLAB with a variable filename in a loop
body: <blockquote>
    <p><strong>Possible Duplicates:</strong><br/>
    <a href="https://stackoverflow.com/questions/5444483/save-mat-matlab">Save mat file from MATLAB</a><br/>
    <a href="https://stackoverflow.com/questions/3048453/how-to-tell-matlab-to-open-and-save-specific-files-in-the-same-directory">How to tell MATLAB to open and save specific files in the same directory</a></p>
  </blockquote>
  
  
  
  <p>I have a set of <a href="http://en.wikipedia.org/wiki/Comma-separated_values" rel="nofollow noreferrer">CSV</a> files and need to extract data to obtain plots. I use the following code to generate a variable file name in a loop and accordingly get the desired data.</p>
  
  <pre><code>P = dir('*.csv');
  
  for m = 1:length(P)
      P(m).data = csvread(P(m).name);
  end
  </code></pre>
  
  <p>I now want to modify these CSV files (change the data values in the CSV files) before obtaining the desired data and then save these files to Excel format (.xls) within the loop.</p>
  
  <p>Something like</p>
  
  <pre><code>for i = 1:length(P(m).data)
      if P(m).data(i,1)&lt; value1
          P(m).data(i,2) = 0;
      end
      save P(m).xls P(m).data -ascii; % Gives error "save 'P(m).data' is not a valid variable name."
  end
  </code></pre>
  
  <p>How do I save a file in Excel (.xls) format with a variable filename obtaining data from array in a loop?</p>