Importing csv files properly
1 view (last 30 days)
Show older comments
Struggling in MATLAB on 17 Jul 2021
Commented: Star Strider on 27 Jul 2021
I am trying to import a csv file which contains 'comma'(,) inside 'quotation' marks(" ") in some cells. Here is one example.
RAB13,"RAB13, member RAS oncogene family",3.042175424,0.009699723
How can I NOT split the data at ',' if they are inside quotation? The csv is attached. Any help is appreciated!
Simon Chan on 18 Jul 2021
You may use readcell and the output is a cell array.
The first row shows the header and the rest of the rows contains the requried data:
rawdata = readcell('test1.csv');
header = rawdata(1,:); % Extract the header
data = rawdata(2:end,:);
Star Strider on 18 Jul 2021
Star Strider on 27 Jul 2021
Following up on that ...
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/687498/test1.csv', 'VariableNamingRule','preserve', 'TextType','string')
Thanks, @Peter Perkins! I hadn’t considered that originally. It definitely improves the result!
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!