MATLAB: How to read in a CSV file containing string that including the delimiter in MATLAB 7.6 (R2008a)

csvdelimiteddelimiterMATLABstringtextread

If I have a CSV file with the following data:
a, A, "a,apple"
b, B, "b,bear"
c, C, "c,cat"
and I read it in using TEXTSCAN, MATLAB parses the fields in the following way:
aA"aapple"
bB"bbear"
cC"ccat"
I would like TEXTSCAN to be able to parse the file in such a way as to ignore occurrences of the delimiter within double-quoted strings, such that the resulting output is
aAa,apple
bBb,bear
cCc,cat

Best Answer

There are several ways of reading in this file.
1. MATLAB's TEXTSCAN function can parse text and ignore delimiters enclosed within double quotation marks (" "). Use the TEXTSCAN function with the '%q' format type to identify strings demarcated by double quotation marks. For example:
str = 'a,A,"a,apple"';
out=textscan(str,'%s%s%q', 'delimiter',',')
This command will generate a cell array 'out' that contains 'a', 'A' and 'a,apple'.
2. If you are on a Windows platform and have Microsoft Excel installed, you can use the following syntax with XLSREAD to read your data into two cell arrays:
[num_data text_data] = xlsread(filename);
After executing this command, the data will be copied to 2 different arrays that treat the data differently:
"num_data" - containing numeric data only; strings and empty fields will be converted to NaN
"text_data" - containing all data read in as strings. The text between two double quotes will be parsed as a single string
3. Create a custom function to parse the file using multiple FREAD or FGETL commands.
For more information on these functions, refer to the documentation by executing the following at the MATLAB command prompt:
doc textscan
doc xlsread
doc fread
doc fgetl