function [data] = csvread (file, sep)
% [data] = csvread (file, [sep])
%
% Read CSV file
%
% file: file name
% sep: separator character [',']
%
% data: matrix of data

  % check parameters
  if nargin < 2
    sep = ',';
  end

  % open file
  fid = fopen(file, 'rt');
  if fid < 0
    error ('can''t open file %s', file);
  end

  % read data
  data = [];
  while 1;
    line = fgets(fid);
    if (~ischar(line))
      break
    end
      
    if ~strcmp(sep, ',')
      line = strrep (line, sep, ',');
    end
    line = strrep (strrep (line, ',,', ',NaN,'), ',,', ',NaN,');
    eval (sprintf('row = [%s];', line));

    % store data into matrix
    if isempty(data)
      data = row;
    elseif columns(data) == length(row)
      data = [data; row];
    else
      error ('can''t read CSV file %s', file);
    end
  end
  
  % close file
  fclose (fid);

% Author: Laurent Mazet
% Email: mazet@softndesign.org
% Date: 2/3/2008

% Local Variables: **
% mode: Matlab **
% End: **