title: Fastest way to import CSV files in MATLAB
body: <p>I've written a script that saves its output to a CSV file for later reference, but the second script for importing the data takes an ungainly amount of time to read it back in. </p>
  
  <p>The data is in the following format:</p>
  
  <pre><code>Item1,val1,val2,val3
  Item2,val4,val5,val6,val7
  Item3,val8,val9
  </code></pre>
  
  <p>where the headers are on the left-most column, and the data values take up the remainder of the row. One major difficulty is that the arrays of data values can be different lengths for each test item. I'd save it as a structure, but I need to be able to edit it outside the MATLAB environment, since sometimes I have to delete rows of bad data on a computer that doesn't have MATLAB installed. So really, part one of my question is: Should I save the data in a different format?</p>
  
  <p>Second part of the question:
  I've tried <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/importdata.html" rel="noreferrer">importdata</a>, <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/csvread.html" rel="noreferrer">csvread</a>, and <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/dlmread.html" rel="noreferrer">dlmread</a>, but I'm not sure which is best, or if there's a better solution. Right now I'm using my own script using a loop and <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fgetl.html" rel="noreferrer">fgetl</a>, which is horribly slow for large files. Any suggestions?</p>
  
  <pre><code>function [data,headers]=csvreader(filename); %V1_1
   fid=fopen(filename,'r');
   data={};
   headers={};
   count=1;
   while 1
        textline=fgetl(fid);
        if ~ischar(textline),   break,   end
        nextchar=textline(1);
        idx=1;
        while nextchar~=','
          headers{count}(idx)=textline(1);
          idx=idx+1;
          textline(1)=[];
          nextchar=textline(1);
        end
        textline(1)=[];
        data{count}=str2num(textline);
        count=count+1;
   end
   fclose(fid);
  </code></pre>
  
  <p>(I know this is probably terribly written code - I'm an engineer, not a programmer, please don't yell at me - any suggestions for improvement would be welcome, though.)</p>