Can't plot a function using character (dates) vector against numeric vector

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"

답변 (1개)

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.

카테고리

도움말 센터File Exchange에서 Calendar에 대해 자세히 알아보기

질문:

2018년 1월 2일

댓글:

2018년 1월 2일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by