csv2cell

Parse CSV files from Excel, mixed data types, to cell array.
6,1K téléchargements
Mise à jour 24 juil. 2008

Aucune licence

% CSV2CELL - parses a Windows CSV file into an NxM cell array, where N is
% the number of lines in the CSV text and M is the number of fields in the
% longest line of the CSV file. Lines are delimited by carriage returns
% and/or newlines.
%
% A Windows CSV file format allows for commas (,) and double quotes (") to
% be contained within fields of the CSV file. Regular fields are just text
% separated by commas (e.g. foo,bar,hello world). Fields containing commas
% or double quotes are surrounded by double quotes (e.g.
% foo,bar,"item1,item2,item3",hello world). In the previous example,
% "item1,item2,item3" is one field in the CSV text. For double quotes to be
% represented, they are written in pairs in the file, and contained within
% a quoted field, (e.g. foo,"this field contains ""quotes""",bar). Spaces
% within fields (even leading and trailing) are preserved.
%
% All fields from the CSV file are returned as strings. If the CSV text
% contains lines with different numbers of fields, then the "missing"
% fields with appear as empty arrays, [], in the returned data. You can
% easily convert the data you expect to be numeric using str2num() and
% num2cell().
%
% Examples:
% >> csv2cell('foo.csv','fromfile') % loads and parses entire file
% >> csv2cell(',,,') % returns cell array {'','','',''}
% >> csv2cell(',,,','text') % same as above, declaring text input
% >> csv2cell(sprintf('%s\r\n%s',...
% '"Ten Thousand",10000,,"10,000","""It''s ""10 Grand"", baby",10k',...
% ',foo,bar,soo'))
% ans =
% 'Ten Thousand' '10000' '' '10,000' [1x22 char] '10k'
% '' 'foo' 'bar' 'soo' [] []
% >> % note the two empty [] cells, because the second line has two fewer
% >> % fields than the first. The empty field '' at the beginning of the
% >> % second line is due to the leading comma on that line, which is
% >> % correct behavior. A trailing comma will do the same to the end of a
% >> % line.
%
% Limitations/Exceptions:
% * This code is untested on large files. It may take a long time due to
% variables growing inside loops (yes, poor practice, but easy coding).
% * This code has been minimally tested to work with a variety of weird
% Excel files that I have.
% * Behavior with improperly formatted CSV files is untested.
% * Technically, CSV files from Excel always separate lines with the pair
% of characters \r\n. This parser will also separate lines that have only
% \r or \n as line terminators.
% * Line separation is the first operation. I don't think the Excel CSV
% format has any allowance for newlines or carriage returns within
% fields. If it does, then this parser does not support it and would not
% return bad output.

Citation pour cette source

Arthur Hebert (2024). csv2cell (https://www.mathworks.com/matlabcentral/fileexchange/20836-csv2cell), MATLAB Central File Exchange. Récupéré le .

Compatibilité avec les versions de MATLAB
Créé avec R2008a
Compatible avec toutes les versions
Plateformes compatibles
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Publié le Notes de version
1.0.0.0