MATLAB: How to convert xtick label to real time? Time is from 18:00 to 23:50 with every 10 minutes interval, so axes for X is from 1 to 36.

datetimextic plot

Best Answer

Which release? Post HG2, use datetime for x axis variable and plot is time-aware. Prior, use datenum for x axis variable and datetick to format the time axis.
dn=datenum(2017,1,1,18,[0:10:36*10-10].',0); % datenum; arbitrary origin
close all
subplot(2,1,1)
plot(dn,randn(size(dn))) % plot datenum data
datetick('x') % show as time
title('datenum/datetick')
dt=datetime(2017,1,1,18,[0:10:36*10-10].',0); % datetime instead
subplot(2,1,2)
plot(dt,randn(size(dn)),'DateTimeTickFormat','HH:mm')
title('datetime/datetick')
results in
ERRATUM Of course, the last title was supposed to be
title('datetime/format')
but not worth more than the note...
ADDENDUM
Rewriting your code--
Npts=36; % points/per trace
Trace1=11; % start with trace number
nPerAxes=3; % traces per axis
nTraces=21; % number traces altogether
nAxes=nTraces/nPerAxes;
i1=(Trace-1)*Npts+1; % magic number for start
i2=i1+Npts*nTraces-1; % and end ditto
data=CHUMPHON_2012(i1:i2,6); % get the data
data=reshape(data,Npts,[]); % put in 2D array Npts X nColumns
dt=datetime(2017,1,1,18,[0:10:Npts*10-10].',0); % 10 min intervals
i1=1; i2=nPerAxes; % now column indices to plot each axes
for i=1:nAxes
hAx(i)=subplot(3,3,i);
plot(dt,data(:,i1:i2),'DateTimeTickFormat','HH:mm')
i1=i2+1; i2=i2+nPerAxes; % increment column indices
end
ADDENDUM 2:
Actually, here's where a duration would be good...remove the arbitrary origin--
du=duration(18,[0:10:36*10-10].',0,'format','hh:mm');
plot(du,randn(size(dn)))
NB: The format string for a duration array is NOT consistent with datetime and plot behaves somewhat differently between the two. It use 'DurationTickFormat' but the allowable format strings aren't the same and unlike datetime, the format property of the duration variable seems to "stick".
Related Question