简体   繁体   中英

X-axis dates are wrong in Matlab plot

My dates are in the format 'yyyymmdd'. When I put these onto the X-axis of my plot, I get crazy numbers. My plot interval is the last decade, but the X-axis tick labels are showing 1979 and other weird numbers. Can anyone point me in the right direction to correct this? Thanks.

Edit: Here is the requested code:

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;

What you are doing is plotting the serial date numbers on the x -axis, when I suspect that you actually want to plot the date strings themselves.

As such, use the serial date numbers to spawn your plot first, then use the date strings by changing the x -axis labels. BTW, your code using str2double is rather pointless because if I follow your comment on the first line of code correctly, this is already a serial date.

Something like this:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM