How to create for loop on date?

23 次查看(过去 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).

回答(2 个)

Star Strider
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 = 4842×2 table
date TAVG _______________________ ____ {'2001-04-01T00:00:00'} 45 {'2001-04-02T00:00:00'} 50 {'2001-04-03T00:00:00'} 59 {'2001-04-04T00:00:00'} 61 {'2001-04-05T00:00:00'} 61 {'2001-04-06T00:00:00'} 61 {'2001-04-07T00:00:00'} 64 {'2001-04-08T00:00:00'} 71 {'2001-04-09T00:00:00'} 65 {'2001-04-10T00:00:00'} 69 {'2001-04-11T00:00:00'} 65 {'2001-04-12T00:00:00'} 69 {'2001-04-13T00:00:00'} 60 {'2001-04-14T00:00:00'} 60 {'2001-04-15T00:00:00'} 66 {'2001-04-16T00:00:00'} 56
Table.date = datetime(Table.date, 'InputFormat','yyyy-MM-dd''T''HH:mm:ss', 'Format','yyyy-MM-dd HH:mm:ss')
Table = 4842×2 table
date TAVG ___________________ ____ 2001-04-01 00:00:00 45 2001-04-02 00:00:00 50 2001-04-03 00:00:00 59 2001-04-04 00:00:00 61 2001-04-05 00:00:00 61 2001-04-06 00:00:00 61 2001-04-07 00:00:00 64 2001-04-08 00:00:00 71 2001-04-09 00:00:00 65 2001-04-10 00:00:00 69 2001-04-11 00:00:00 65 2001-04-12 00:00:00 69 2001-04-13 00:00:00 60 2001-04-14 00:00:00 60 2001-04-15 00:00:00 66 2001-04-16 00:00:00 56
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}')
.

Walter Roberson
Walter Roberson 2023-1-14
S = dateshift(datetime('now') - days(300), 'start', 'day')
S = datetime
20-Mar-2022
for D = S : calmonths(1) : S + calmonths(5)
D
end
D = datetime
20-Mar-2022
D = datetime
20-Apr-2022
D = datetime
20-May-2022
D = datetime
20-Jun-2022
D = datetime
20-Jul-2022
D = datetime
20-Aug-2022
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
  1 个评论
Behrooz Daneshian
Behrooz Daneshian 2023-1-14
Sorry I get confused. Please take a look at the attached table. Would you please explain on that?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by