Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to Plot Stock Returns on a Chart

32 views
Skip to first unread message

Ryan

unread,
Nov 15, 2012, 8:31:19 AM11/15/12
to
Hello everyone. I am trying to get this code working:

% [num,txt] = xlsread('C:\Program Files\MATLAB\R2012a\StockSymbols.xls')
% or another alternive: Browse file location

[f, p] = uigetfile({'*.xls';'*.xlsx'},'Pick StockSymbols file');
filename=fullfile(p,f);
[num,txt] = xlsread(filename);

% Get stocks
DATE = txt(2:end,1); % Date begins from second row

% Sort data in case dates are not in chronological order
[DATE_num,sortIndex] = sort(datenum(DATE,'mm/dd/yy')); % no hour,minute and second in your date data

BA = num(sortIndex,1);
BAC = num(sortIndex,2);
CAT = num(sortIndex,3);
CSCO = num(sortIndex,4);
DIS = num(sortIndex,5);
GE = num(sortIndex,6);
HD = num(sortIndex,7);
IBM = num(sortIndex,8);
JPM = num(sortIndex,9);
KFT = num(sortIndex,10);
KO = num(sortIndex,11);
MMM = num(sortIndex,12);
MSFT = num(sortIndex,13);
PFE = num(sortIndex,14);
PG = num(sortIndex,15);
T = num(sortIndex,16);
VZ = num(sortIndex,17);
XOM = num(sortIndex,18);

% DATE_num = datenum(DATE,'dd/mm/yy'); % no hour,minute and second in your date data
hf=figure('Units','normalized','Position',[0.05 0.05 0.9 0.7]);
plot ( DATE_num , BA , 'r'), hold on, grid on % hold if you want to plot on the same figure, add grid
title('Stock Symbols');
xlabel('Dates');
ylabel('Stock Symbols ($)');

plot ( DATE_num , BAC, 'b')
plot ( DATE_num , CAT ,'k')
plot ( DATE_num , CSCO, 'g')
plot ( DATE_num , DIS, 'm')
plot ( DATE_num , GE, 'c')

% Add legend if wanted
legend('BA','BAC','CAT','CSCO','DIS','GE','Location','Best')

% Set axes labels
mindate=min(DATE_num);
maxdate=max(DATE_num);
ha=get(hf,'CurrentAxes');
set(ha,'XTickLabelMode','manual','XTickMode','manual','XLimMode','manual',...
'XLim',[mindate-15 maxdate+15],'XTick',linspace(mindate,maxdate,10));
% % % xticklabels=datestr(get(ha,'XTick'),24); % Will be used to label the X axis, format is dd/mm/yyyy
% % % set(ha,'XTickLabel',xticklabels);
% % %
dateaxis('x',1);



All it does not is open a chart, but nothing gets plotted. I don't get any errors.

Below is a sample file to download and put in the Matlab folder (so you don't have to spend a lot of time creating your another Excel file).
http://www.mediafire.com/view/?gjd29zoo7p4c8kw

Phil Goddard

unread,
Nov 15, 2012, 10:10:19 AM11/15/12
to
Your spreadsheet does not have the same format as the format required by the code you show.
Your first column are numbers not dates (yes they represent dates, but they are already numbers).
Hence the txt variable only has one row (the column headers) and
>> DATE = txt(2:end,1);
is empty.
Hence your data variables are empty and there is nothing to plot.

You need to change your initial date/data processing to reflect your actual data file.

Phil.

Ryan

unread,
Nov 15, 2012, 10:28:05 PM11/15/12
to
Oh, yeah, you are right. I thought those dates had to numbers. Anyway, how did you determine what the problem was. I was stepping through the code; using Breakpoints and tapped the F11 key so many times...I couldn't figure it out. How did you figure it out???


"Phil Goddard" <ph...@goddardconsulting.ca> wrote in message <k830kr$ili$1...@newscl01ah.mathworks.com>...

Phil Goddard

unread,
Nov 16, 2012, 12:40:23 PM11/16/12
to

I executed

[num,txt] = xlsread('StockSymbols.xls');

and noticed that txt only had one row in it => no dates.
Then I opened the Excel file (in Excel not MATLAB) and confirmed that there are no dates represented as text.

Phil.

Ryan

unread,
Nov 16, 2012, 10:31:06 PM11/16/12
to
OK! Thanks for the help with this!! I really appreciate it!!

"Phil Goddard" <ph...@goddardconsulting.ca> wrote in message <k85tq6$hi0$1...@newscl01ah.mathworks.com>...
0 new messages