How to create for loop on date?
22 次查看(过去 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 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!