Importing csv files properly

조회 수: 5 (최근 30일)
Struggling in MATLAB
Struggling in MATLAB 2021년 7월 17일
댓글: Star Strider 2021년 7월 27일
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