Timetable - loop through days to check for a condition occurring during midday the day prior to midday of the current day

조회 수: 18 (최근 30일)
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.

채택된 답변

Eric Sofen
Eric Sofen 2021년 5월 3일
As a starting point, take a look at the retime function.
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
Eric Sofen
Eric Sofen 2021년 5월 3일
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!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Calendar에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by