Matlab 图中的 X 轴日期错误

X-axis dates are wrong in Matlab plot

我的日期格式是 'yyyymmdd'。当我将这些放在绘图的 X 轴上时,我得到了疯狂的数字。我的绘图间隔是最近十年,但 X 轴刻度标签显示 1979 年和其他奇怪的数字。谁能指出我正确的方向来纠正这个问题?谢谢

编辑:这是请求的代码:

TradeDate=TradeDate+693960; % convert excel serial dates to matlab serial dates
TradeDate=datestr(TradeDate,'mmddyyyy'); % convert the format into yyyymmdd
TradeDate=str2double(cellstr(TradeDate)); % convert the date strings first into cell arrays and then into a double

plot(TradeDate,beta);
xlabel('Date');
ylabel('Beta');
daspect([300 1 1]);
set(gca,'xtick',linspace(TradeDate(1),TradeDate(1715),50));
ax=gca;
ax.XTickLabelRotation=45;

你正在做的是在 x 轴上绘制序列日期数字,而我怀疑你实际上想自己绘制日期字符串。

因此,首先使用日期序列号生成您的绘图,然后通过更改 x 轴标签使用日期字符串。顺便说一句,您使用 str2double 的代码毫无意义,因为如果我正确地按照您对第一行代码的评论,这是 已经 一个序列日期。

像这样:

TradeDate=TradeDate+693960; % convert excel serial dates to matlab serial dates

%// Note change in variable name here and convert to cell array of strings
TradeDates=cellstr(datestr(TradeDate,'mmddyyyy')); % convert the format into mmddyyy

plot(TradeDate,beta);
xlabel('Date');
ylabel('Beta');
daspect([300 1 1]);
set(gca,'xtick',linspace(TradeDate(1),TradeDate(1715),50));
%// Change - Change x-axis labels
set(gca, 'XTickLabel', TradeDates(1:50:1715));
ax=gca;
ax.XTickLabelRotation=45;