Daniel@0: function data = csv2cell(varargin) Daniel@0: % CSV2CELL - parses a Windows CSV file into an NxM cell array, where N is Daniel@0: % the number of lines in the CSV text and M is the number of fields in the Daniel@0: % longest line of the CSV file. Lines are delimited by carriage returns Daniel@0: % and/or newlines. Daniel@0: % Daniel@0: % A Windows CSV file format allows for commas (,) and double quotes (") to Daniel@0: % be contained within fields of the CSV file. Regular fields are just text Daniel@0: % separated by commas (e.g. foo,bar,hello world). Fields containing commas Daniel@0: % or double quotes are surrounded by double quotes (e.g. Daniel@0: % foo,bar,"item1,item2,item3",hello world). In the previous example, Daniel@0: % "item1,item2,item3" is one field in the CSV text. For double quotes to be Daniel@0: % represented, they are written in pairs in the file, and contained within Daniel@0: % a quoted field, (e.g. foo,"this field contains ""quotes""",bar). Spaces Daniel@0: % within fields (even leading and trailing) are preserved. Daniel@0: % Daniel@0: % All fields from the CSV file are returned as strings. If the CSV text Daniel@0: % contains lines with different numbers of fields, then the "missing" Daniel@0: % fields with appear as empty arrays, [], in the returned data. You can Daniel@0: % easily convert the data you expect to be numeric using str2num() and Daniel@0: % num2cell(). Daniel@0: % Daniel@0: % Examples: Daniel@0: % >> csv2cell('foo.csv','fromfile') % loads and parses entire file Daniel@0: % >> csv2cell(',,,') % returns cell array {'','','',''} Daniel@0: % >> csv2cell(',,,','text') % same as above, declaring text input Daniel@0: % >> csv2cell(sprintf('%s\r\n%s',... Daniel@0: % '"Ten Thousand",10000,,"10,000","""It''s ""10 Grand"", baby",10k',... Daniel@0: % ',foo,bar,soo')) Daniel@0: % ans = Daniel@0: % 'Ten Thousand' '10000' '' '10,000' [1x22 char] '10k' Daniel@0: % '' 'foo' 'bar' 'soo' [] [] Daniel@0: % >> % note the two empty [] cells, because the second line has two fewer Daniel@0: % >> % fields than the first. The empty field '' at the beginning of the Daniel@0: % >> % second line is due to the leading comma on that line, which is Daniel@0: % >> % correct behavior. A trailing comma will do the same to the end of a Daniel@0: % >> % line. Daniel@0: % Daniel@0: % Limitations/Exceptions: Daniel@0: % * This code is untested on large files. It may take a long time due to Daniel@0: % variables growing inside loops (yes, poor practice, but easy coding). Daniel@0: % * This code has been minimally tested to work with a variety of weird Daniel@0: % Excel files that I have. Daniel@0: % * Behavior with improperly formatted CSV files is untested. Daniel@0: % * Technically, CSV files from Excel always separate lines with the pair Daniel@0: % of characters \r\n. This parser will also separate lines that have only Daniel@0: % \r or \n as line terminators. Daniel@0: % * Line separation is the first operation. I don't think the Excel CSV Daniel@0: % format has any allowance for newlines or carriage returns within Daniel@0: % fields. If it does, then this parser does not support it and would not Daniel@0: % return bad output. Daniel@0: % Daniel@0: % Copyright 2008 Arthur Hebert Daniel@0: Daniel@0: % Process arguments Daniel@0: if nargin == 1 Daniel@0: text = varargin{1}; Daniel@0: elseif nargin == 2 Daniel@0: switch varargin{2} Daniel@0: case 'fromfile' Daniel@0: filename = varargin{1}; Daniel@0: fid = fopen(filename); Daniel@0: text = char(fread(fid))'; Daniel@0: fclose(fid); Daniel@0: case 'text' Daniel@0: text = varargin{1}; Daniel@0: otherwise Daniel@0: error('Invalid 2nd argument %s. Valid options are ''fromfile'' and ''text''',varargin{2}) Daniel@0: end Daniel@0: else Daniel@0: error('CSV2CELL requires 1 or 2 arguments.') Daniel@0: end Daniel@0: Daniel@0: Daniel@0: % First split it into lines Daniel@0: lines = regexp(text,'(\r\n|[\r\n])','split'); % lines should now be a cell array of text split by newlines Daniel@0: Daniel@0: % a character is either a delimiter or a field Daniel@0: inField = true; Daniel@0: inQuoteField = false; Daniel@0: % if inField && ~inQuoteField --> then we're in a raw field Daniel@0: Daniel@0: skipNext = false; Daniel@0: data = {}; Daniel@0: field = ''; Daniel@0: for lineNumber = 1:length(lines) Daniel@0: nChars = length(lines{lineNumber}); % number of characters in this line Daniel@0: fieldNumber = 1; Daniel@0: for charNumber = 1:nChars Daniel@0: if skipNext Daniel@0: skipNext = false; Daniel@0: continue Daniel@0: end Daniel@0: thisChar = lines{lineNumber}(charNumber); Daniel@0: if thisChar == ',' Daniel@0: if inField Daniel@0: if inQuoteField % this comma is part of the field Daniel@0: field(end+1) = thisChar; Daniel@0: else % this comma is the delimiter marking the end of the field Daniel@0: data{lineNumber,fieldNumber} = field; Daniel@0: field = ''; Daniel@0: fieldNumber = fieldNumber + 1; Daniel@0: end Daniel@0: else % we are not currently in a field -- this is the start of a new delimiter Daniel@0: inField = true; Daniel@0: end Daniel@0: if charNumber == nChars % this is a hanging comma, indicating the last field is blank Daniel@0: data{lineNumber,fieldNumber} = ''; Daniel@0: field = ''; Daniel@0: fieldNumber = fieldNumber + 1; Daniel@0: end Daniel@0: elseif thisChar == '"' Daniel@0: if inField Daniel@0: if inQuoteField Daniel@0: if charNumber == nChars % it's the last character, so this must be the closing delimiter? Daniel@0: inField = false; Daniel@0: inQuoteField = false; Daniel@0: data{lineNumber,fieldNumber} = field; Daniel@0: field = ''; Daniel@0: fieldNumber = fieldNumber + 1; Daniel@0: else Daniel@0: if lines{lineNumber}(charNumber+1) == '"' % this is translated to be a double quote in the field Daniel@0: field(end+1) = '"'; Daniel@0: skipNext = true; Daniel@0: else % this " is the delimiter ending this field Daniel@0: data{lineNumber,fieldNumber} = field; Daniel@0: field = ''; Daniel@0: inField = false; Daniel@0: inQuoteField = false; Daniel@0: fieldNumber = fieldNumber + 1; Daniel@0: end Daniel@0: end Daniel@0: else % this is a delimiter and we are in a new quote field Daniel@0: inQuoteField = true; Daniel@0: end Daniel@0: else % we are not in a field. This must be an opening quote for the first field? Daniel@0: inField = true; Daniel@0: inQuoteField = true; Daniel@0: end Daniel@0: else % any other character ought to be added to field Daniel@0: field(end+1) = thisChar; Daniel@0: if charNumber == nChars Daniel@0: data{lineNumber,fieldNumber} = field; Daniel@0: field = ''; Daniel@0: fieldNumber = fieldNumber + 1; Daniel@0: elseif charNumber == 1 % we are starting a new raw field Daniel@0: inField = true; Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: