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?

回答(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 个评论

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... :)

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Dates and Time 的更多信息

产品

版本

R2016a

标签

Community Treasure Hunt

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

Start Hunting!

Translated by