如何在 Matlab 中最好地设置日期轴

How to best set date axis in Matlab

你能帮我在 Matlab 中设置日期轴还是指向右边 post?

我的问题如下: 我有一些数字格式的价格和日期要绘制,例如:

 Prices = repmat([10; 5; 3; 4; 11; 12; 5; 2],10,1);

 Dates = [726834:726834+8*10-1]';

如果我这样绘制它们:

 plot(Dates,Prices)
 dateaxis('x',17)

我得到了我不想要的 x 轴值,因为它们看起来不规则(我猜它们遵循某些规则但看起来不太好)。我怎样才能最好地将它们设置为,例如,始终是每月的第一天,或者一月的第一天和七月的第一天,等等?我知道我可以使用 set(gca, 'xtick', ?? ??);但我缺乏关于我究竟如何做到这一点的概述,而且 Matlab 帮助对我没有帮助。

此代码用每个月的第一天标记绘图。要获取每年的一月或七月,只应 selected 月数组的某些元素。该策略是使用 eomdate 获取每个月的最后一天并加 1。图 1 给出每个月的第一天,图 2 给出数组 select 中的月份 months_to_display .

Prices = repmat([10; 5; 3; 4; 11; 12; 5; 2],10,1);

Dates = [726834:726834+8*10-1]';

firstDate = strsplit(datestr(Dates(1)-1, 'dd,mm,yyyy'),',');
lastDate = strsplit(datestr(Dates(end), 'dd,mm,yyyy'),',');

months = mod(str2double(firstDate{2}):str2double(lastDate{2})+12*(str2double(lastDate{3})-str2double(firstDate{3})),12);
months(months == 0) = 12;

years = zeros(1,length(months));
currYear = str2double(firstDate{3});
for i = 1:length(months)
    years(i) = currYear;
    if (months(i) == 12)
        currYear = currYear + 1;
    end
end

dayCount = eomdate(years,months);
firstDates = dayCount+1;

figure(1)
plot(Dates, Prices)
xticks(firstDates);
xticklabels(datestr(firstDates));

months_to_display = [1 7];
months_to_display = months_to_display - 1;
months_to_display(months_to_display == 0) = 12;
months_to_collect = ismember(months, months_to_display);

months = months(months_to_collect);
years = years(months_to_collect);

dayCount = eomdate(years,months);
firstDates = dayCount+1;

figure(2)
plot(Dates, Prices)
xticks(firstDates);
xticklabels(datestr(firstDates));