Assign date values manually to datetime array matlab

1 view (last 30 days)
Hi all, I have this date data (attached) where I need to assign the day numbers (manually) as 1, 2 etc using the starting date (in this example '2021-02-05 05:02:00' as day 1 and '2021-03-07 04:57:00' as say 31). I use Matlab function "day(data)" where it gives me the day numbers based on calender date ('2021-03-07 04:57:00' as day 5). Could you please help me to assign the date manually.
Thanks in advance

Accepted Answer

Rik
Rik on 8 Apr 2021
For pre-R2020b, you can use readfile, which you can get readfile from the FEX. If you are using R2017a or later, you can also get it through the AddOn-manager.
date_list=readlines('https://www.mathworks.com/matlabcentral/answers/uploaded_files/576897/Date.txt');
date_list=strrep(date_list,'''','');%remove quotes
date_list=datetime(date_list,'InputFormat','yyyy-MM-dd HH:mm:ss')
date_list = 31×1 datetime array
05-Feb-2021 05:02:00 06-Feb-2021 04:56:00 07-Feb-2021 05:29:00 08-Feb-2021 04:25:00 09-Feb-2021 03:55:00 10-Feb-2021 04:50:00 11-Feb-2021 03:30:00 12-Feb-2021 04:03:00 13-Feb-2021 04:12:00 14-Feb-2021 04:23:00 15-Feb-2021 04:14:00 16-Feb-2021 05:38:00 17-Feb-2021 04:23:00 18-Feb-2021 03:31:00 19-Feb-2021 03:06:00 20-Feb-2021 05:10:00 21-Feb-2021 04:04:00 22-Feb-2021 03:02:00 23-Feb-2021 04:29:00 24-Feb-2021 03:37:00 25-Feb-2021 03:22:00 26-Feb-2021 04:28:00 27-Feb-2021 05:16:00 28-Feb-2021 04:50:00 01-Mar-2021 04:16:00 02-Mar-2021 02:34:00 03-Mar-2021 05:06:00 04-Mar-2021 04:47:00 05-Mar-2021 04:40:00 06-Mar-2021 04:35:00
day_numbers=days(date_list-min(date_list))+1
day_numbers = 31×1
1.0000 1.9958 3.0187 3.9743 4.9535 5.9917 6.9361 7.9590 8.9653 9.9729
You can use round, floor, or ceil if you want round numbers.
  6 Comments
Rik
Rik on 9 Apr 2021
You can round before subtracting the lowest to use only the day.

Sign in to comment.

More Answers (0)

Categories

Find more on Dates and Time in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!