2

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');
4
  • The little table you show is obviously not representative of a CSV. Show a sample of the input file, the first few lines should be enough. Commented Oct 29, 2012 at 4:43
  • I did edit my original post to show sample input Commented Oct 29, 2012 at 4:53
  • If the line containing the word DATA is part of the file get rid of it, csvread only reads numbers. And if it isn't, get rid of it, it's misleading people trying to help you. Commented Oct 29, 2012 at 5:01
  • @HighPerformanceMark Sorry, I'd already typed most of my answer when your comment came through, so I figured I would still post it. Commented Oct 29, 2012 at 5:07

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I get the following error ??? Error using ==> datenum at 178 DATENUM failed. Error in ==> Q3 at 7 FinalData = [datenum(Data{1}, 'mm/dd/yyyy'), Data{2}, Data{3}, Data{4}, Data{5}, Data{6}, Data{7}]; Caused by: Error using ==> dtstr2dtnummx Failed on converting date string to date number.
@gianteagle Apologies - it is fixed. I forgot about the header lines. By the way, that "DATA" you've got above your sample isn't actually in the csv file is it (if it is, delete it)? Also, I just made it a bit more compact then.
Thanks for the input. One last issue. The date column gets printed as 734800
@gianteagle That was a deliberate choice by me. Type datestr(734800) into your Command Window. You will notice you get the appropriate date. Matlab can't store date strings and numerical data in one numerical matrix. The solution is to convert all dates to a numerical format. 734800 is the number of days since January 0, 0000 AD. If you're planning on using Matlab, I suggest you get used to using the numerical date format. It really is much more flexible once you get the hang of it. If you want all the dates in string format, use DateVec = datestr(D(:, 1)).
0

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".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.