problem with dateshift..how solve it?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
data1=datetime("2023-01-01")
data2=datetime("2023-11-15")
dt=data1:days(7):data2
RP=dateshift(data1:calmonths(1):data2, 'end', 'month');
RP = dateshift(RP,'dayofweek','Saturday','previous');
RP(end)
why RP(end) is > data2 ?
i write: dt=data1:days(7):data2 the result must to be <=data2 but it's > data2
0 Kommentare
Antworten (2)
Walter Roberson
am 20 Okt. 2023
dt=data1:days(7):data2 does end before data2 -- the last entry is the 12-Nov-2023 that you see displayed.
But then you take something that starts at the beginning of the month of January 2023, and advance one calendar month at a time ending no later than data2 . The beginning of Novemeber 2023 is several calendar months after January 1 2023, so the data1:calmonths(1):data2 series ends with a November 1 2023 datetime object. you then dateshift() that to the end of the month, getting a November 30, 2023 datetime object. You then dateshift() that to the previous Saturday, which gets you the November 25 2023 datetime object. There is no reason why the last Saturday of a month must be before a datetime that is mid-month.
7 Kommentare
Stephen23
am 21 Okt. 2023
Most likely FIND is not required, logical indexing is simpler and more efficient:
RP(RP<dt(end))
Mann Baidi
am 20 Okt. 2023
Hi Luca,;
I understnad you are facing issues in getting the desired output using the "dateshift" function. You are encountering the above issue because when the code is executed, the array "RP" has the end value as "30-Nov-2023" after the execution of line 4.
data1=datetime("2023-01-01");
data2=datetime("2023-11-15");
dt=data1:days(7):data2;
RP=dateshift(data1:calmonths(1):data2, 'end', 'month')
RP = dateshift(RP,'dayofweek','Saturday','previous');
RP(end);
This is the reason "RP(end)" is giving "25-Nov-2023" as it is the previous saturday for "30-Nov-2023".
However, if you would like to avoid this issue and would like to find the previous Saturday of "data2" you can modify the code by passing "RP(end-1)" instead of "RP(end)" in line 5 of the code., and then check for the "data2"separately. Here is the possible workaround.
data1=datetime("2023-01-01");
data2=datetime("2023-11-15");
dt=data1:days(7):data2;
RP=dateshift(data1:calmonths(1):data2, 'end', 'month');
RP = dateshift(RP(end-1),'dayofweek','Saturday','previous');
RP(end)
lastSat= dateshift(data2,'dayofweek','Saturday','previous')
Hope this will resolve your doubt and query!
Siehe auch
Kategorien
Mehr zu Calendar finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!