Kulis
Kulis

Reputation: 1010

Import .csv to Matlab

I try to load this .csv:

Time    Column01    Column02    Column03    
11.03.2004  09:17:02    236.81  237.31  236.45
11.03.2004  09:17:05    236.6   237.06  236.18
11.03.2004  09:17:08    236.67  237.16  236.28
11.03.2004  09:17:12    236.83  237.24  236.41
11.03.2004  09:17:15    233.84  234.2   233.4
...

using:

b=csvimport('u1.csv','columns',{'Time'});
c=csvimport('u1.csv','columns',{'Column03'});

first work, but second no. Anybody know why? I have this error:

Error using csvimport (line 200) Cannot locate column header 'Column03' in the file 'u1.csv'. Column header names are case sensitive.

Error in TSFP (line 4) c=csvimport('u1.csv','columns',{'Column03'});

Here is description about this function.

Upvotes: 0

Views: 199

Answers (2)

jpjacobs
jpjacobs

Reputation: 9549

There are no commas in your file!

So strictly speaking, it's not a comma separated value file. From the looks of it, it's tab separated instead.

Probably you'll have better results using dlmread as in the first example:

M = gallery('integerdata', 100, [5 8], 0); 
dlmwrite('myfile.txt', M, 'delimiter', '\t')
dlmread('myfile.txt')
ans =
    96    77    62    41     6    21     2    42
    24    46    80    94    36    20    75    85
    61     2    93    92    82    61    45    53
    49    83    74    42     1    28    94    21
    90    45    18    90    14    20    47    68

Upvotes: 3

sclarke81
sclarke81

Reputation: 1769

I'd use textscan for this if you want to import all of the data. dlmread will only import numeric data, but your date and time columns need to be treated as strings.

filename='u1.csv';
fid=fopen(filename,'r');
C=textscan(fid, '%s%s%f%f%f', 'HeaderLines', 1);
fclose(fid);

C is a cell array, so to access the second value in the date column and the second value in Column01 you'd do this:

date = C{1}{2};
value = C{3}(2);

It's worth noting that this imports the date and time separately because there is whitespace between them in the file. If you wanted to recombine them you could do this:

C={strcat(C{1}, {' '}, C{2}), C{3:5}};

Upvotes: 1

Related Questions