Read a CSV file
13 views (last 30 days)
Show older comments
Govind Sankar Madhavan Pillai Ramachandran Nair
on 14 Jan 2019
Commented: Guillaume
on 14 Jan 2019
I have a CSV file that is auto generated from my LabVIEW program. Even though it is a CSV file the LabVIEW program uses semicolon as the delimeter. Now I want to import this CSV file into MATLAB. There are total of 27 rows and 32 columns. The first row consists completely of string values that are the headings, i.e temp1, temp2, temp3 etc upto temp 32. Then from row 2 to row 27 are numeric values. How can I import both the string values and numeric values in the format 27x32. It can be cell or struct. But how to achieve this.
2 Comments
Rik
on 14 Jan 2019
I would read the file in two steps: reading the headers separately from the rest of the data.
Answers (1)
Guillaume
on 14 Jan 2019
Probably the simplest is to use readtable to import the data as a table. readtable should be smart enough to recognise that the first row is a header used to name the columns and use that to name the variables of the table. readtable will also have no issue recognising that the separator is a semicolon, so:
t = readtable(yourfile)
is probably all that is needed. If not, the options of readtable can be used to fix the problem, or if really pushed, detectImportOptions.
3 Comments
Jeremy Hughes
on 14 Jan 2019
The function detectImportOptions is available in R2016b. In R2015b and earlier, readtable always assumed ',' was the delimiter, and that there were no header lines. You'll have to set these appropriate to the content of the file. Without seeing a file, that's as close to an answer as I can give.
Guillaume
on 14 Jan 2019
Possibly, try:
B = readtable(filename, 'Delimiter', ';')
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!