How to create for loop on date?
24 views (last 30 days)
Show older comments
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 Comments
Answers (2)
Star Strider
on 14 Jan 2023
Edited: Star Strider
on 15 Jan 2023
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 Comments
Walter Roberson
on 14 Jan 2023
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
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!