Importing csv files properly

7 次查看(过去 30 天)
Struggling in MATLAB
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!

回答(2 个)

Simon Chan
Simon Chan 2021-7-18
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
Star Strider 2021-7-18
Import it as a table, using readtable
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/687498/test1.csv', 'VariableNamingRule','preserve')
T1 = 3×4 table
Gene.symbol Gene.title logFC adj.P.Val ___________ _____________________________________________________________ _______ _________ {'CEACAM6'} {'carcinoembryonic antigen related cell adhesion molecule 6'} 5.4297 0.0086381 {'RAB13' } {'RAB13, member RAS oncogene family' } 3.0422 0.0096997 {'TMOD3' } {'tropomodulin 3' } -1.7502 0.0096997
  2 个评论
Peter Perkins
Peter Perkins 2021-7-27
Add "TextType","string" to that readtable call and you have a winner!
Star Strider
Star Strider 2021-7-27
Following up on that ...
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/687498/test1.csv', 'VariableNamingRule','preserve', 'TextType','string')
T1 = 3×4 table
Gene.symbol Gene.title logFC adj.P.Val ___________ ___________________________________________________________ _______ _________ "CEACAM6" "carcinoembryonic antigen related cell adhesion molecule 6" 5.4297 0.0086381 "RAB13" "RAB13, member RAS oncogene family" 3.0422 0.0096997 "TMOD3" "tropomodulin 3" -1.7502 0.0096997
Thanks, @Peter Perkins! I hadn’t considered that originally. It definitely improves the result!
.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by