Borrar filtros
Borrar filtros

How to correctly read in a csv file using csvread?

203 visualizaciones (últimos 30 días)
Aidan Goy
Aidan Goy el 23 de Nov. de 2020
Comentada: Rik el 7 de Dic. de 2020
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
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 comentario
Rik
Rik el 7 de Dic. de 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.

Iniciar sesión para comentar.

Respuestas (1)

Star Strider
Star Strider el 23 de Nov. de 2020
Since you have R2020b, use readtable instead.
(Also, note that Update 2 has recently been released.)
  2 comentarios
Aidan Goy
Aidan Goy el 23 de Nov. de 2020
Is it possible to do it without using readtable and instead using csvread?
Star Strider
Star Strider el 23 de Nov. de 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.

Iniciar sesión para comentar.

Categorías

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

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by