How to locate the index of a certain date for a date-time array?
17 views (last 30 days)
Show older comments
Sevde Tavasli on 9 Dec 2022
Commented: Sevde Tavasli on 10 Dec 2022
I have a date-time array, 'dt' that is recorded hourly for a whole year.
The first three data points are as follows, 01-Jan-2017 00:00:00, 01-Jan-2017 01:00:00, 01-Jan-2017 02:00:00.
I would like to find the index of every Monday(at 00:00:00) present in the data set . Below is what I have used, this is not feasable as I would need to know the exact date of every Monday, type it out row by row and save it to my matrix m.
Is there a better way to do this?
What I am trying to achieve with this is to create new arrays that start on a Monday and last for a whole week, and do this for the span of a whole year.
I tried to use
but it does not start on a Monday, so I was not able to get it to work.
Stephen23 on 10 Dec 2022
You are working with datetimes, so do not compare text!
dt = datetime(2017,1,1,(0:1:123).',0,0, 'Format','u MM dd eee HHmmss')
ix = timeofday(dt)==0 & weekday(dt)==2
md = dt(ix)
More Answers (1)
Arif Hoq on 10 Dec 2022
I don't have your data. try this one:
% creating datetime data from 1st January 2017 to 31 December 2017
firstdate = '01-01-2017 00:00:00';
t1 = datetime(firstdate,'InputFormat','dd-MM-yyyy HH:mm:ss');
t2 = datetime(lastdate,'InputFormat','dd-MM-yyyy HH:mm:ss');
Find more on Dates and Time 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!