Assign date values manually to datetime array matlab

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

 Risposta accettata

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 Commenti

Hi Rik thanks for your help, much appreciated. I have used
day_numbers=floor(day_numbers); to get the day numbers from 1 to 31.
I am going to accept this answer!!
Hi Rik, thanks for helping me with day number analysis. While doing the analysis I found that some of the days would be repeated (day 25 twice, 24 twice etc) for some dates. Please find attached the .mat file which contains the dates. Please note that '05-Feb-2021' needs to be assigned as the first day and '07-Mar-2021' to be assigned as day 31 and other dates in between. Please note that this sequence is repeated for every person and some may have 10 days, 28 days etc. But first day and day 31 information remains the same. I would really appreciate availability of any better solultion on this, thanks.
What is your question? Why is my solution not suitable? If you have two measurements for one day, why would that influence the number assigned to that day? You also didn't attach a mat file.
Ganesh Naik
Ganesh Naik il 9 Apr 2021
Modificato: Ganesh Naik il 9 Apr 2021
Hi Rik sorry for not attaching the file before. For my data each person will have their data from day 1 to day 31 ('05-Feb-2021' as days 1 and '07-Mar-2021' as day 31, some subjects may have few days of data only). Hence, one subject will not have same 2 days of data. Maximum a subject can have is 31 days of data. But in my case some subjects data goes upto 34 or 32 days due to some days repeated (rounding problem). I tried with floor, ceil etc but still the same problem. Your solution is good and hence, /i have accepted it but for me I need to address the data going beyond 31 days for each subject etc. I have attached the file now.
Note: Also, let me check the file carefully again. Since it is a large dataset the results maybe correct.
You can round before subtracting the lowest to use only the day.
Hi Rik thanks, I will look into it

Accedi per commentare.

Più risposte (0)

Categorie

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by