How to correctly read in a csv file using csvread?
206 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have a CSV file I need to read in and I need to use csvread to do this, I cant seem to figure out how to get it to read in correctly as when I read in it saves as a 407x1 array and does not include the various headings from my csv file which looks like this
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/424808/image.png)
The full CSV has 407 rows and 9 columns. I need read the csv file to a point where I am then able to work with each specific column in order to create graphs etc but I have been struggling for quite some time now to get this read in correctly. I also have the option to save as a text file and use fopen fclose fread etc if that would make my task easier?
Thanks
1 commentaire
Rik
le 7 Déc 2020
Regarding your flag: you received answers on both questions, so I don't think you should delete either question. Please don't post duplicates in the future.
Réponses (1)
Star Strider
le 23 Nov 2020
(Also, note that Update 2 has recently been released.)
2 commentaires
Star Strider
le 23 Nov 2020
I seriously doubt it, given the file contents displayed. In general, .csv files contain only numeric data, although csvread allows for a header line (and the ability to skip it). For your file, readtable appears tro be the only viable option.
Tables are realtively easy to work with, although if you are unfamiliar with them, they can require a bit of initial effort to understand. See for example Access Data in Tables, and other documentation in and linked to in the table documentation. If you only want to use the numeric data initially, that is certainly possible. Later, if you need the text data, it will be there for you to use.
I will help you work with the data in your table, if you need help. I certainly encourage you to use readtable. Be sure to use ,'VariableNamingRule','preserve' in the readtable call so the variable names (column headers) are imported without change. To then use those those variable names, put them in single quotes, so if ‘T’ is your table, to refer to ‘car name’:
Name = T.('car name');
and so for any of the others with similar variable names. To refer to the other variables, the normal conventions apply.
Voir également
Catégories
En savoir plus sur String Parsing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!