Timetable - loop through days to check for a condition occurring during midday the day prior to midday of the current day
9 views (last 30 days)
Show older comments
Hi all,
I was after some help with my fitbit fitness tracker data. I'm still trying to get my head around timetables (and Matlab in general) so go easy on me!
I have a timetable with a row for each minute (timestamp is in this format: '01-Dec-2019 00:00:00'). The timetable also has several columns of different activity states ('sleep' 'sed' 'lpa' 'mpa' 'vpa') where the value in each cell is either a 1 or 0 depending on the activity state for that minute and the total across the row will always be 1. For example for minute '01-Dec-2019 00:00:00' the columns would look like sleep=1 sed=0 lpa=0 mpa=0 vpa=0 when the participant was asleep during that minute.
Now to deem a day as valid, amongst several conditions, there needs to be any amount of sleep (>=1 rows scored as sleep) between midday of that day and midday of the day prior. So for '01-Dec-2019' to be valid there needs to be at least a single row scored as sleep '30-Nov-2019 12:00:00' and '01-Dec-2019 12:00:00'. I also need to loop this through every day of my dataset (roughly 12 months). I'm looking to write this loop so that the resulting variable is a daily column of logical responses (0 or 1) to the question "is there any sleep from midday yesterday to midday today?".
The start date doesn't matter for now, I will always have a full day prior to my date range of interest to allow for testing this condition.
I hope I've made sense and provided enough detail.
Accepted Answer
Eric Sofen
on 3 May 2021
I think the easiest way to do this is create a copy of the timetable, shift the time by 12 hours so that a day's sleep falls within a calendar day.
ttShifted = tt(:,'sleep');
ttShifted.date = ttShifted.date+hours(12);
Then use retime to find the total amount of sleep/day, then figure out if it's more than an hour. Note that this assumes that your data is always once/minute.
ttCountSleep = retime(ttShifted, 'daily', 'sum')
ttValidDay = ttCountSleep.sleep > 60;
2 Comments
Eric Sofen
on 3 May 2021
Actually, now I see that you just needed one row of non-zero sleep, so the second part is just ttCountSleep>0. Maybe I need to get more sleep!
More Answers (0)
See Also
Categories
Find more on Language Fundamentals in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!