Importing csv files properly

5 Ansichten (letzte 30 Tage)
Struggling in MATLAB
Struggling in MATLAB am 17 Jul. 2021
Kommentiert: Star Strider am 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!

Antworten (2)

Simon Chan
Simon Chan am 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
Star Strider am 18 Jul. 2021
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 Kommentare
Peter Perkins
Peter Perkins am 27 Jul. 2021
Add "TextType","string" to that readtable call and you have a winner!
Star Strider
Star Strider am 27 Jul. 2021
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!
.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Tables finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by