Can't plot a function using character (dates) vector against numeric vector
Show older comments
Hi Matlab buddies.
I have a problem plotting a simple line plot. I think it's because i converted my Excel Dates to Matlab Dates (which happens to create charactor vector).
Hope you guys can help me out. And thanks in advance.
%%Import the data
[~, ~, raw] = xlsread('/Users/hassanismael/Documents/MATLAB/Plotting/SP500_PI.xlsx','Sheet1');
raw = raw(2:end,:);
%%Create output variable
data = reshape([raw{:}],size(raw));
%%Allocate imported array to column variable names
MATLABDate = x2mdate(data(:,1), 0);
date = datestr(MATLABDate);
Cons_Discre = data(:,2);
Cons_Staples = data(:,3);
Energy = data(:,4);
Financials = data(:,5);
HealthCare = data(:,6);
Industrials = data(:,7);
Inf_Tech = data(:,8);
Materials = data(:,9);
Tele_Serv = data(:,10);
Utilities = data(:,11);
%%Clear temporary variables
clearvars data raw;
plot(date,Energy) % "error using plot invalid first data argument"
Answers (1)
Walter Roberson
on 2 Jan 2018
If you have R2016b or later, use
date = datetime(MATLABDate, 'ConvertFrom', 'datenum');
If not, then use
date = MATLABDate;
and also call datetick() to set up the tick labels properly.
1 Comment
Hassan Ismael
on 2 Jan 2018
Categories
Find more on Calendar 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!