MATLAB Answers

Tea
0

Excluding date from date and time

Asked by Tea
on 26 May 2019
Latest activity Commented on by Peter Perkins
on 4 Jun 2019
Hello,
I am fascing difficulties with plotting data. My data are containg measurements of heart rate. First column is string with date and time, and second column are values of heart rate. I have converted first column into datetime values using following code:
day89 = datenum(heartrate_89.start_time, 'yyyy-mm-dd HH:MM:SS.FFF');
ts2 = day89;
ts2 = datetime(ts2,'TimeZone','local',...
'ConvertFrom','datenum');
day89=ts2
I have measurements for couple of days and since all of them are staring from 20:00 and last till 10:00, I was wondering if there is a way to plot a graph starting from 20:00 untill 10:00, containing traces for couple of days, because when I try to plot different traces on same graph, since the dates are different, they are far away from each other?
Is there a way to completely exclude date value since I am only interested in time?
Thank you

  2 Comments

can you post a excerpt of the data?
Tea
on 26 May 2019
Yes, for example, data are starting from 8th of May (20:00) and finishing on 9th of May (around 10:00), which is one night.
I have couple of nights starting from same time and ending at same time.
I hope this picture will help.

Sign in to comment.

2 Answers

Answer by Rik
on 26 May 2019
 Accepted Answer

You can probably also do this with the datetime class, but when you still have your time in the datenum format, you can simply subtract the floor to keep only time.
If you want to keep the traces continuous you can add 1 to values befor 10 AM.
day89 = datenum(heartrate_89.start_time, 'yyyy-mm-dd HH:MM:SS.FFF');
%crop to 1 day
day89 = day89-floor(day89);
L= day89 <= 10/24;
day89(L)=day89(L)+1;
ts2 = day89;
ts2 = datetime(ts2,'TimeZone','local',...
'ConvertFrom','datenum');
day89=ts2

  1 Comment

Tea
on 26 May 2019
Thank you, this solved my problem.

Sign in to comment.


Answer by the cyclist
on 26 May 2019
Edited by the cyclist
on 26 May 2019

You should be able to use the timeofday function.
The resulting output will be a duration array.

  1 Comment

the cyclist is correct. While Rik's trick works, sort of, it also creates datetimes that are in the (ISO) year -1.
>> dn = now
dn =
7.3758e+05
>> dn0 = dn - floor(dn)
dn0 =
0.39464
>> dt = datetime(dn0,'ConvertFrom','datenum')
dt =
datetime
31-Dec--0001 09:28:17
That will eventually come back to bit you. durations are the way to go.

Sign in to comment.