Importing csv files properly

7 visualizaciones (últimos 30 días)
Struggling in MATLAB
Struggling in MATLAB el 17 de Jul. de 2021
Comentada: Star Strider el 27 de Jul. de 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!

Respuestas (2)

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

Iniciar sesión para comentar.

Categorías

Más información sobre Tables en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by