gianteagle
gianteagle

Reputation: 47

Error when reading data from csv file into MATLAB

I have the following sample data

Date,Open,High,Low,Close,Volume,Adj Close
10/24/2011,181.51,183.39,180.62,182.25,5890600,182.25
10/21/2011,179.11,181.67,178.75,181.63,8054200,181.63
10/20/2011,178.13,179.24,176.17,177.25,7513800,177.25

I am reading data from a CSV file into MATLAB.

data = csvread('filename.csv','A1:G12542');

I get the following errors. Any idea how to circumvent this issue ?

   ??? Error using ==> dlmread at 145
Header lines must be integer-valued.

Error in ==> csvread at 50
    m=dlmread(filename, ',', r, c);

Error in ==> Q11 at 1
data1 = csvread('ibm.csv','A1:G12542');

Upvotes: 2

Views: 6432

Answers (2)

Andy Campbell
Andy Campbell

Reputation: 2187

In 13b you can leverage a MATLAB table to access this data:

data = readtable('filename.csv');
data = 

    Date         Open      High      Low      Close       Volume      AdjClose
____________    ______    ______    ______    ______    __________    ________

'10/24/2011'    181.51    183.39    180.62    182.25    5.8906e+06    182.25  
'10/21/2011'    179.11    181.67    178.75    181.63    8.0542e+06    181.63  
'10/20/2011'    178.13    179.24    176.17    177.25    7.5138e+06    177.25 

Note there was a warning I have omitted about the fact that the variable name was modified to be a valid MATLAB identifier. This was because of the space between "Adj Close".

Upvotes: 0

Colin T Bowers
Colin T Bowers

Reputation: 18580

Three problems:

1) The sample data provided is NOT csv. csv stands for comma separated variable. Your sample data appears to be separated by tabs or whitespace. UPDATE: I see you've edited your sample so that the data is now csv. I've edited my solution accordingly.

2) If you wish to use the row and column input arguments to csvread they should be input as zero-based integer valued row and column arguments, not an "excel" type string.

3) From the sample you provide, it appears that you want to obtain the dates in the first column. This is a problem if you want to use csvread or dlmread as both of these functions can handle numeric data only (note that 10/24/2011 is a string).

So, I'm going to assume that what you've shown us is representative of your file. Given this, the way to import it into matlab is as follows:

fid1 = fopen('YourFilePathHere', 'r');
D = textscan(fid1, '%s%f%f%f%f%f%f', 'Delimiter', ',', 'HeaderLines', 1);
fclose(fid1)
D = [datenum(D{1}, 'mm/dd/yyyy'), D{2}, D{3}, D{4}, D{5}, D{6}, D{7}];

In the first line, I open the file for reading. In the second line I scan in the data. Note that format string '%s%f%f%f%f%f%f'. This says that the first column is string format %s, and the next 6 columns are floating point (we could alter this to other numeric types, but floating point is simplest for now). In the third line I close the file. In the final line, I convert the date strings to matlab numerical date format, and then combine this with the rest of the data into one numerical matrix, ready for analysis.

Upvotes: 1

Related Questions