如何增加 datetick 中显示的 xticks 数量?

how to increase the number of xticks displayed in datetick?

在下面的代码中,我想将每个子图中 x 轴上的刻度数从 2 增加到 4(或者通常增加到我想要的任何数字)。目前,它只显示 12 am-12 am。任何帮助将不胜感激。

x=rand(96,1);
y=rand(96,1);
z=rand(96,1);

Ts=900;


t = 0:Ts:24*3600-Ts; %time in second
time = datestr(t/86400+datenum(2014,1,1)); %time starting at 2014/01/01

subplot(1,3,1)
ts_x = timeseries(x, time);
plot(ts_x)
datetick('x','HHPM')
ax = gca;
ax.XTickLabelRotation = 90;
set(gca,'fontsize',16)


subplot(1,3,2)
ts_y = timeseries(y, time);
plot(ts_y)
datetick('x','HHPM')
ax = gca;
ax.XTickLabelRotation = 90;
set(gca,'fontsize',16)


subplot(1,3,3)
ts_z = timeseries(y, time);
plot(ts_z)
datetick('x','HHPM')
ax = gca;
ax.XTickLabelRotation = 90;
set(gca,'fontsize',16)

这里有一种方法,比如有 5 个刻度:

 xt=ax.XTick;
 ax.XTick=linspace(xt(1),xt(end),5);
 ax.XTickLabels=datestr(ax.XTick,'HHPM');

只需在代码中定义 ax 之后放置它即可。