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!

采纳的回答

Star Strider
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 个评论
noquinhas
noquinhas 2018-10-31
Thank you! Do you know how I can add a timedate object to a timeseries? When I try the following:
mytimeseries = timeseries(mysignal, ts(1:end-1)); %So it has the same length as the signal since ts has a length of (n+1)
It gives me the following error:
Error using timeseries/init
The second argument must be either the time vector or the time series name.
Thank you once again!
Star Strider
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 CenterFile Exchange 中查找有关 Time Series 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by