How to extract elements from datetime variable

I have a datetime variable of 1048576x1 elements, I can convert the format to various options, including showing the days of the week (which is what I am interested in). However, I need to find certain elements in the variable corresponding to a specific day. How can this be done?

Answers (2)

REad about datevec. With this you can get year, month, day, hour, minute and second information.
To get the weekday from a datetime variable use weekday. Unfortunately this returns the weekday with Sunday=1 (and thus does not match ISO 8601 or most of the world), but a simple mod call could fix that.
Or take mod of a serial date number to get the day of the week:
1+mod(datenumber-3,7)

3 Comments

but how do I search for a specific day? My data is sampled every minute and I have a couple of years' worth of data, so I have 1440 elements for each day and ~900 days' data. I need to identify, e.g. all Tuesdays within the data set? The complexity is that I can't use the number of elements as I have some missing data so odd number of elements with respect to each day.
"but how do I search for a specific day?"
If you want to check if a particular day exists in your datetime array or weekday then just use a logical comparison, or ismember:
weekday(yourarray)==3
Much obliged, you are an officer and a gentleman... :)

Sign in to comment.

Categories

Products

Release

R2016a

Tags

Asked:

on 1 Aug 2018

Commented:

on 1 Aug 2018

Community Treasure Hunt

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

Start Hunting!