p.matsinopoulos
p.matsinopoulos

Reputation: 7810

How to use readtable in Matlab to read CSV file with double quote as text delimiter?

I have a CSV file that includes both numeric and string data. String data are surrounded with double quotes so that they can include commas as part of the actual string value.

Example:

2,false,false,15,"Foo String,Value",30,true,false,20

Can I use readtable() to load this CSV file? And if "yes", how can I do that?

Is there any other suggestion as a better alternative?

Upvotes: 1

Views: 1226

Answers (1)

Thomas Ibbotson
Thomas Ibbotson

Reputation: 745

You can specify a format string for readtable, which follows the same rules as textscan. The magic %q format specifier reads in a double-quoted string, which prevents the commas from being interpreted as delimiters. I think the following would work for your example:

t = readtable('mycsvfile.txt', 'Format', '%d%s%s%d%q%d%s%s%d', 'Delimiter', ',');

Upvotes: 1

Related Questions