How to create for loop on date?
19 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (2)
Star Strider
el 14 de En. de 2023
Editada: Star Strider
el 15 de En. de 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 comentarios
Walter Roberson
el 14 de En. de 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
Ver también
Categorías
Más información sobre Dates and Time en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!