MATLAB: How to convert serial date numbers to date and time format in a timeseries

datetimetimeseries

I'm trying to convert my values of Time in the format 'dd-mmm-yyyy HH:MM:SS', but somehow my time series keep changing the date.
I have a signal with a sampling frequency of 200 Hz (so 1 second "will be" 200 samples). The signal started to record at a certain date and time and I'm trying to create a timeseries to show me the "true" time and not samples (where the recording starts from zero).
% Create timeseries
ts = timeseries(mysignal);
% Set start/stop date and time of recording
start = '20-Aug-2018 07:25:13';
stop = '20-Aug-2018 11:42:54';
ts.TimeInfo.Startdate = start;
By default, my timeseries will be:
ts.Time(1) = '20-Aug-2018 07:25:13';
ts.Time(2) = '20-Aug-2018 07:25:14';
when, in reality, the date and time '20-Aug-2018 07:25:14' will only be at the 201-st sample.
I've adjusted my timeseries to fit this by doing the following:
% Convert time and date
starttime = datenum(start);
endtime = datenum(stop);
% Set a new interval
tsout = setuniformtime(ts, 'StartTime', starttime, 'EndTime', endtime);
But the date in ts is changing to:
tsout.Time(1) = '26-Aug-2018 20:13:23';
tsout.Time(end) = '26-Aug-2018 20:13:23';
How can I set a new interval and display it in the format I want? My goal is to have:
ts.Time(1) = '20-Aug-2018 07:25:13';
...
ts.Time(201) = '20-Aug-2018 07:25:14';
...
ts.Time(end) = '20-Aug-2018 11:42:54';
I also followed the same strategy as here but since my signal is quite large, datestr is very slow at making the conversion.
Thank you!

Best Answer

Try this:
start = '20-Aug-2018 07:25:13';
stop = '20-Aug-2018 11:42:54';
stop = '20-Aug-2018 07:25:15'; % Shorter Series For Test
t1 = datetime(start, 'InputFormat','dd-MMM-yyyy HH:mm:ss')
t2 = datetime(stop, 'InputFormat','dd-MMM-yyyy HH:mm:ss')
ts = (t1:seconds(1/200):t2)';
ts.Format = 'dd-MMM-yyyy HH:mm:ss.SSS';
T1 = ts(1) % Check Results (Delete Later)
T201 = ts(201)
TEND = ts(end)
Producing:
T1 =
20-Aug-2018 07:25:13.000
T201 =
20-Aug-2018 07:25:14.000
TEND =
20-Aug-2018 07:25:15.000