How to have a different interval between xticks and xticklabels on a datetime plot?

I have measurements at 2-minute frequency for a period of 41 days and want to plot a variable over that period with an xtick mark appearing at each midnight, but an xticklabel (i.e. date info) appearing every other midnight i.e. the data start on July 31st, i'd like the next label to be 2nd Aug (missing 1st Aug) etc
I've looked at the DatetimeRuler property help, but the code below produces an xtick mark plus label for each midnight (one ticklabel is not 'skipped').
Following other help entries, my approach has been to create a string variable for the xticklabel that has blank values for every other midnight label, but this doesn't produce the hoped-for plot when DatetimeRuler is being used. Thanks for any help.
start_dt=datetime('2018-07-31 09:42:00');
end_dt=datetime('2018-09-09 09:40:00');
x_datetimes=(start_dt:minutes(2):end_dt); %Create a 2-minute interval for the time period
start_date=dateshift(start_dt,'start','day'); %Find start date
end_date=dateshift(end_dt,'start','day');
x_period_dates=(start_date:days(1):end_date); %Build list of n=41 days' dates
midnight=duration('00:00:00','InputFormat','hh:mm:ss'); %Create a midnight duration
x_dates_midnights=datetime((x_period_dates+midnight),'Format','yyyy-MM-dd HH:mm:ss'); %Make n=41 date+midnight datetimes
x_dates_midnights_Wgaps_str=string(x_dates_midnights); %Make a string variable for dates with midnights
x_dates_midnights_Wgaps_str(2:2:end)=''; %Replace every other date/midnight string with empty
figure;
plot(x_datetimes,rand(1,length(x_datetimes))); %Plot using a datetime on x-axis
xmin = min(x_dates_midnights);
xmax = max(x_dates_midnights);
xtix = xmin:days(1):xmax;
ax=gca;
ax.XAxis.TickValues=xtix-1; % -1 to not show final tick
ax.XAxis.TickLabel=x_dates_midnights_Wgaps_str;
xtickformat('MMM-dd HH:mm')

 Accepted Answer

The x_period_dates variable is all you need. It's a datetime vector containing all of the dates at midnight. You just need to set the axes xtick values to every odd index of x_period_dates to label every second day.
figure;
plot(x_datetimes,rand(1,length(x_datetimes))); %Plot using a datetime on x-axis
ax = gca;
ax.XTick = x_period_dates(1:2:end)
To show a tick for every day at midnight and label every second day, one idea is to follow the code block above by setting the minor tick values.
ax.XAxis.MinorTickValues = x_period_dates;
Alternatively, you can set the xticks and xticklabels independently.
figure;
plot(x_datetimes,rand(1,length(x_datetimes))); %Plot using a datetime on x-axis
ax = gca;
ax.XTick = x_period_dates;
xdateLabels = string(x_period_dates);
xdateLabels(2:2:end) = "";
ax.XTickLabel = xdateLabels;

4 Comments

Here's some additional feedback as inline comments that start with "FEEDBACK".
start_dt=datetime('2018-07-31 09:42:00');
end_dt=datetime('2018-09-09 09:40:00');
x_datetimes=(start_dt:minutes(2):end_dt); %Create a 2-minute interval for the time period
start_date=dateshift(start_dt,'start','day'); %Find start date
end_date=dateshift(end_dt,'start','day');
x_period_dates=(start_date:days(1):end_date); %Build list of n=41 days' dates
% FEEDBACK: these next 2 lines are not needed. x_period_dates are already
% at midnight.
midnight=duration('00:00:00','InputFormat','hh:mm:ss'); %Create a midnight duration
x_dates_midnights=datetime((x_period_dates+midnight),'Format','yyyy-MM-dd HH:mm:ss'); %Make n=41 date+midnight datetimes
% FEEDBACK: no need to convert to strings. Use the datetime ticks rather
% than overwriting those tick labels.
x_dates_midnights_Wgaps_str=string(x_dates_midnights); %Make a string variable for dates with midnights
x_dates_midnights_Wgaps_str(2:2:end)=''; %Replace every other date/midnight string with empty
figure;
plot(x_datetimes,rand(1,length(x_datetimes))); %Plot using a datetime on x-axis
xmin = min(x_dates_midnights);
xmax = max(x_dates_midnights);
xtix = xmin:days(1):xmax;
ax=gca;
% FEEDBACK: xtix-1 shifts all dates back by 1 day. It does not remove
% the final tick.
ax.XAxis.TickValues=xtix-1; % -1 to not show final tick
ax.XAxis.TickLabel=x_dates_midnights_Wgaps_str;
xtickformat('MMM-dd HH:mm')
Thanks Adam, the answer and the extra feedback are very helpful. You've kindly explained the tick labelling for me, but in my original request I also wanted a tick mark at every midnight, along with those every-other labels. Therefore, ticks required at midnight for all days Jul 31, Aug 1, Aug 2, Aug 3, Aug 4 etc., but ticklabels only for midnight Jul 31, Aug 2, Aug 4 I tried your suggestions and it doesn't seem to achieve that tick vs. labelling requiremenent yet?
Thanks for the reminder! I've updated my answer with two approaches to show a daily tick and label every second date.

Sign in to comment.

More Answers (1)

"xtick mark appearing at each midnight, but an xticklabel (i.e. date info) appearing every other midnight"
Using xtickformat sets all the tick labels, overwriting your previously set tick labels (where every other one was empty). Avoid using xtickformat, and instead set the Format of x_dates_midnights to 'MMM-dd HH:mm'.
start_dt=datetime('2018-07-31 09:42:00');
end_dt=datetime('2018-09-09 09:40:00');
x_datetimes=(start_dt:minutes(2):end_dt); %Create a 2-minute interval for the time period
start_date=dateshift(start_dt,'start','day'); %Find start date
end_date=dateshift(end_dt,'start','day');
x_period_dates=(start_date:days(1):end_date); %Build list of n=41 days' dates
midnight=duration('00:00:00','InputFormat','hh:mm:ss'); %Create a midnight duration
x_dates_midnights=datetime((x_period_dates+midnight),'Format','MMM-dd HH:mm'); %Make n=41 date+midnight datetimes
x_dates_midnights_Wgaps_str=string(x_dates_midnights); %Make a string variable for dates with midnights
x_dates_midnights_Wgaps_str(2:2:end)=''; %Replace every other date/midnight string with empty
figure;
plot(x_datetimes,rand(1,length(x_datetimes))); %Plot using a datetime on x-axis
xmin = min(x_dates_midnights);
xmax = max(x_dates_midnights);
xtix = xmin:days(1):xmax;
ax=gca;
ax.XAxis.TickValues=xtix-1; % -1 to not show final tick
ax.XAxis.TickLabel=x_dates_midnights_Wgaps_str;

Products

Release

R2022b

Asked:

on 12 Apr 2024

Commented:

on 15 Apr 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!