Let us say I have a series of dates: '01-01-2022', '05-01-2022', '06-01-2022' and I have corresponding data on these dates how do i convert the dates to an array that looks like this: [0,4,5]
Generally something like this:

 Akzeptierte Antwort

Simon Chan
Simon Chan am 18 Mär. 2022

1 Stimme

Use function days
DateStrings = {'01-01-2022','05-01-2022','06-01-2022'};
t = datetime(DateStrings,'InputFormat','dd-MM-yyyy');
days(t-t(1))
ans = 1×3
0 4 5

Weitere Antworten (2)

Stephen23
Stephen23 am 18 Mär. 2022
Bearbeitet: Stephen23 am 18 Mär. 2022

1 Stimme

Your example and explanation are inconsistent: your explanation shows difference between adjacent dates, your example vector shows differences to the first date. Here are both:
C = {'01-01-2022', '05-01-2022', '06-01-2022'};
T = datetime(C,'inputFormat','d-M-u');
V = days(T-T(1)) % what your example shows
V = 1×3
0 4 5
D = [0,caldays(caldiff(T))] % what you explained
D = 1×3
0 4 1
KSSV
KSSV am 18 Mär. 2022

0 Stimmen

Read about datevec. This will give you respective days from the dates.

3 Kommentare

DateString = {'09/16/2007';'05/14/1996';'11/29/2010'};
formatIn = 'mm/dd/yyyy';
datevec(DateString,formatIn)
yes I found the above but how do i get the number from this? It is easy when they are in the same month but what if it is like above? Different month and year?.
KSSV
KSSV am 18 Mär. 2022
Bearbeitet: KSSV am 18 Mär. 2022
DateString = {'09/16/2007';'05/14/1996';'11/29/2010'};
formatIn = 'mm/dd/yyyy';
[y,m,d,H,M,S] = datevec(DateString,formatIn);
d
d = 3×1
16 14 29
Hamza Yusuf
Hamza Yusuf am 18 Mär. 2022
I am sorry this is not what i wanted. Look at the original post. I want a vector that says how many days between the dates like this:
[0, days from date 1 to date 2, days from date 2 to date 3...., days from date n-1 to date n]

Melden Sie sich an, um zu kommentieren.

Kategorien

Produkte

Version

R2020b

Tags

Gefragt:

am 18 Mär. 2022

Bearbeitet:

am 18 Mär. 2022

Community Treasure Hunt

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

Start Hunting!

Translated by