Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to Plot Stock Returns on a Chart

Subject: How to Plot Stock Returns on a Chart

From: Ryan

Date: 15 Nov, 2012 13:31:19

Message: 1 of 5

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

Subject: How to Plot Stock Returns on a Chart

From: Phil Goddard

Date: 15 Nov, 2012 15:10:19

Message: 2 of 5

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.

Subject: How to Plot Stock Returns on a Chart

From: Ryan

Date: 16 Nov, 2012 03:28:05

Message: 3 of 5

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" <phil@goddardconsulting.ca> wrote in message <k830kr$ili$1@newscl01ah.mathworks.com>...
> 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.

Subject: How to Plot Stock Returns on a Chart

From: Phil Goddard

Date: 16 Nov, 2012 17:40:23

Message: 4 of 5

 
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.

Subject: How to Plot Stock Returns on a Chart

From: Ryan

Date: 17 Nov, 2012 03:31:06

Message: 5 of 5

OK! Thanks for the help with this!! I really appreciate it!!

"Phil Goddard" <phil@goddardconsulting.ca> wrote in message <k85tq6$hi0$1@newscl01ah.mathworks.com>...
>
> 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.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us