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
Rik on 14 Jan 2019
I would read the file in two steps: reading the headers separately from the rest of the data.

Sign in to comment.

Answers (1)

Guillaume
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

B = readtable(filename)
Error using table/readTextFile (line 237)
Cannot interpret data in the file 'Temp1.csv'. Found 1 variable names but 33 data columns. You
may need to specify a different format string, delimiter, or number of header lines.
Error in table.readFromFile (line 33)
t = table.readTextFile(filename,otherArgs);
Error in readtable (line 118)
t = table.readFromFile(filename,varargin);
This is what I am getting when I am using readtable. Also I dont have detectImportOptions since I am using MATLAB 2014. Is there anywhere from which I can download the .m file for detectImportOptions.
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.
Possibly, try:
B = readtable(filename, 'Delimiter', ';')

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!