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 个)
Walter Roberson
2018-1-2
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 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!