How to correctly read in a csv file using csvread?
197 次查看(过去 30 天)
显示 更早的评论
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 个评论
Rik
2020-12-7
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.
回答(1 个)
Star Strider
2020-11-23
(Also, note that Update 2 has recently been released.)
2 个评论
Star Strider
2020-11-23
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 String Parsing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!