How to create for loop on date?
조회 수: 7 (최근 30일)
이전 댓글 표시
Hi all,
Suppose that I have a table having two columns(variables). The first column is date and the second one is daily tempretaure. How can I make a for loop on dates? (e.g. lets say from september 2019 to April 2020, do this task on tempretaure column).
댓글 수: 0
답변 (2개)
Star Strider
2023년 1월 14일
편집: Star Strider
2023년 1월 15일
If you want to simply isolate the dates from September 2019 to April 2020, ise the isbetween function (or logical indexing, essentially what the function does). You can then copy them to a separate table to work with them. No loop is required.
EDIT — (15 Jan 2023 ar 1:19)
Perhaps something like this —
LD = load(websave('Table','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1264110/Table.mat'));
Table = LD.Table
Table.date = datetime(Table.date, 'InputFormat','yyyy-MM-dd''T''HH:mm:ss', 'Format','yyyy-MM-dd HH:mm:ss')
Select = isbetween(Table.date, datetime(2019,09,01), datetime(2020,04,3));
figure
plot(Table{Select,1}, Table{Select,2})
grid
xlim([min(Table{Select,1}) max(Table{Select,1})])
xlabel('Date & Time')
ylabel('T_{AVG}')
.
댓글 수: 0
Walter Roberson
2023년 1월 14일
S = dateshift(datetime('now') - days(300), 'start', 'day')
for D = S : calmonths(1) : S + calmonths(5)
D
end
However...
If you are wanting to loop over months for grouping purposes, then much of the time it is easier to convert the table to a timetable and then use retime .
Or, alternately, to use ymd to extract year and month, use caldiff to calculate months relative to the ear, after which you can findgroups
참고 항목
카테고리
Help Center 및 File Exchange에서 Calendar에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
