How to convert serial date numbers to date and time format in a timeseries?
4 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
Star Strider
2018-10-30
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
2 个评论
Star Strider
2018-10-31
As always, my pleasure!
I am not certain what you want to do. Perhaps a timetable (link) object is what you are looking for. (It was introduced in R2016b.) I do not have much experience with timetable objects, although they look straightforward to implement.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Time Series 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!