function ImportCSV(Filename)
%ImportCSV  import CSV file
%
%    ImportCSV(Filename) imports a comma-separated value text file into
%    Matlab.  This file may have numeric or text values.  Each column is
%    imported into a separate cell array.
%
%    Example: ImportCSV('ears_overview.txt')
%
%    Kevin C. Eagar
%    February 25, 2009
%    Last Updated: 02/25/2009

if ~ischar(Filename)
    error('Filename MUST be a string');
end
if ~exist(Filename,'file')
    error(['File ' Filename ' does not exist.']);
end

fid = fopen(Filename,'r');
while 1
    if feof(fid); break; end
    TextLine = fgetl(fid);
    for n = 1:length(findstr(',',TextLine));
        if ~exist(['Column' num2str(n,'%02.0f')],'var')
            [NewColumn{1},TextLine] = strtok(TextLine,',');
        else
            eval(['NewColumn = Column' num2str(n,'%02.0f') ';']);
            [NewColumn{end+1},TextLine] = strtok(TextLine,',');
        end
        eval(['Column' num2str(n,'%02.0f') ' = NewColumn;']);
        clear('NewColumn')
    end
end

Outputs = who('Column*');
for n = 1:length(Outputs)
    assignin('base',Outputs{n},eval(Outputs{n}));
end