Excel: Column Headers & Dates
16 views (last 30 days)
Show older comments
A short time ago, I was fiddling around with some methods of importing data from an Excel file, specifically, stock prices. I found some useful information here: http://www.mathworks.com/help/matlab/ref/xlsread.html
Of all the techniques listed, I didn't find any that would import column headers; you just have all the values in lots of rows and columns, but no way to identify which stock is which. Also, dates seemed to become Text, and when I tried to plot the nameless stocks (column headers just fell off), there was no dates on the X-axis. The version that I was using before; R2010b. I just saw a video demo online; it was definitely a newer version of Matlab. In the new version, it seems like all data is imported automatically; no issues at all. Dates come in fine; column headers show up fine. Whenever I tried to import any stock data, the 'create vectors from each column using column names' was ALWAYS greyed out. It can't be so difficult. Any idea what was going wrong???
0 Comments
Answers (6)
per isakson
on 19 Apr 2012
The documentation (R2012a) says:
[num,txt,raw] = xlsread(filename)
...
[num,txt,raw] = xlsread(filename,sheet,range)
...
I'm sure this has not changed during the last few years. The text, e.g. column headers, of the excel-file appears in both the second, i.e. txt, and third, i.e. raw, output argument.
Beware: Headers (of type General in Excel) with numeric values, e.g. 1234, confuses xlsread (and sometimes me). Row headers, i.e. text or datetime in the first column, cause the column indices of num and txt not to line up. In this case it might be more robust to use the third output argument
[~,~,raw] = xlsread(filename)
Make some experiments with a small xls-file.
--- EDIT in response to Answer 6 ---
I have downloaded stock_data.xls and run the following six lines of code line by line.
file_spec = 'h:\m\cssm\stock_data.xls';
[ num, txt, raw ] = xlsread( file_spec );
whos
num( 1:3, : )
txt{1,:}
raw{1,:}
The results are:
>> whos
Name Size Bytes Class Attributes
file_spec 1x24 48 char
num 575x4 18400 double
raw 576x5 352498 cell
txt 576x5 334098 cell
The size of the numerical array, num, is smaller than that of the two cell arrays.
>> num( 1:3, : )
ans =
30.8100 4.4500 71.9400 61.6700
30.9800 4.2900 72.2200 60.6300
30.3500 4.1900 72.1200 59.2900
The variable, num, only holds numeric data and the column and row indices differs from those in the Excel-sheet.
>> txt{1,:}
ans =
Dates
ans =
MSFT
ans =
PWER
ans =
KO
ans =
SBUX
The first row of txt contains the headers! The first row of raw is identical to that of txt.
>> txt{2:3,1}
ans =
2012-04-13
ans =
2012-04-12
The first column of txt contains the dates. The specific date format depends on the "localization" of Windows.
I run this in with R2012a on Windows7 with Excel installed. The result will be the same with R2010b and previous versions of Windows and Excel.
0 Comments
owr
on 19 Apr 2012
The latest version of MATLAB (2012A) seems to have some better support. I havent personally played with this yet though.
If you are dealing with nicely formatted Excel files where each column has a header and you have heterogeneous columns (mixed type - like text for dates and numeric for price data), check out the "dataset arrays" in the Statistics Toolbox (if you have access to it). The dataset array class has a constructor that can read directly from Excel (or any delimited text file) that works great for this sort of thing.
0 Comments
Image Analyst
on 20 Apr 2012
I have no idea what your data looks like. Can you give the header row (with the column headers in it) and a couple of rows of actual data, or else (even better) post your workbook somewhere online that we can download. Then we'd be able to try it and fix it for you.
0 Comments
Ryan
on 20 Apr 2012
1 Comment
Image Analyst
on 20 Apr 2012
When you have a response to an answer, please add it on as a "Comment on this Answer" so we know whom you're talking to.
Ryan
on 20 Apr 2012
Edited: John Kelly
on 2 Mar 2015
3 Comments
Eshu Jain
on 17 Jan 2016
Hi
I am also facing a similar issue. I have data of 50 stocks closing price for entire year 2015. Now using [num, txt, raw] i could get the complete data with headers in raw. However I want to know how to store this data in the a matrix of different name. Or is the by default matrix for the entire data with header names is 'raw' ?
Walter Roberson
on 17 Jan 2016
Eshu, if you are using R2014b or later, I recommend that you use readtable() to read the XLS file into a MATLAB table() data structure. (Table() data structures exist from R2013b but readtable was not created at the same time.)
See Also
Categories
Find more on Data Import from MATLAB 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!