Add next and previous business date of each date in array row

2 ビュー (過去 30 日間)
chiefjia
chiefjia 2021 年 10 月 8 日
コメント済み: C B 2021 年 10 月 9 日
Dear MATLAB experts,
I have a table named 'events', which contains many unique dates and I want to get the previous and next business dates of each date (row) included in this table. If one row were t, then I would like to get t-1 and t+1 (in business days), without these newly created rows replacing already existing ones, but creating one new row for each one of the newly created dates. I have thought of the code below, but it doesn't work so far:
% Create array
eventsWindow3 = table2array(events);
% Iterate through previous and next business date
for i=1:length(eventsWindow3)
eventsWindow3(i-1,:) = busdate(eventsWindow3(i,:), -1);
eventsWindow3(i+1,:) = busdate(eventsWindow3(i,:), 1);
end
You can find 'events' attached to this table.
I would really appreciate your help, thank you in advance.

採用された回答

C B
C B 2021 年 10 月 8 日
編集済み: C B 2021 年 10 月 8 日
@chiefjia will this work?
% Create array
EventsArray = [datetime('today') datetime('yesterday') datetime('tomorrow')]
newArray = [];
% Iterate through previous and next business date
for i=1:length(EventsArray)
newArray{end+1} = EventsArray(i)-1;
newArray{end+1} = EventsArray(i);
newArray{end+1} = EventsArray(i)+1;
end
newArray
EventsArray =
1×3 datetime array
08-Oct-2021 07-Oct-2021 09-Oct-2021
newArray =
1×9 cell array
Columns 1 through 5
{[07-Oct-2021 00:00:00]} {[08-Oct-2021]} {[09-Oct-2021 00:00:00]} {[06-Oct-2021 00:00:00]} {[07-Oct-2021]}
Columns 6 through 9
{[08-Oct-2021 00:00:00]} {[08-Oct-2021 00:00:00]} {[09-Oct-2021]} {[10-Oct-2021 00:00:00]}
  2 件のコメント
chiefjia
chiefjia 2021 年 10 月 8 日
Hi Chetan,
thanks a lot for your response, this works. I've also adapted your suggestion to my code and this is what I got:
% Create array to iterate through previous and next business date
eventsWindow = table2array(events); % For setting up different event windows
eventsWindow3 = [];
% For loop that adds a row for each one of the business dates
for i=1:length(eventsWindow)
eventsWindow3{end+1} = cellstr(datetime(busdate(eventsWindow(i), -1), 'ConvertFrom', 'datenum'));
eventsWindow3{end+1} = eventsWindow(i);
eventsWindow3{end+1} = cellstr(datetime(busdate(eventsWindow(i), 1),'ConvertFrom', 'datenum'));
end
C B
C B 2021 年 10 月 9 日
Nice to see you worked it out.

サインインしてコメントする。

その他の回答 (1 件)

KSSV
KSSV 2021 年 10 月 8 日
d = datetime('today')
d = datetime
08-Oct-2021
d0 = d-day(1)
d0 = datetime
07-Oct-2021
d1 = d+day(1)
d1 = datetime
09-Oct-2021
  3 件のコメント
KSSV
KSSV 2021 年 10 月 8 日
You are running a loop for each event right? Then you have the date.
chiefjia
chiefjia 2021 年 10 月 8 日
Yes, I am running a loop for each event, which is a row of the array eventsWindow3, but still I can't apply your solution

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by