HOW TO FILTER AN HOUR DATA FROM ARRAY OF DAILY,MONTHLY AND YEAR DATA

1 次查看(过去 30 天)
I HAVE DATA WITH FOLLOWING COLUMN: DATE, TIME, S/N, VALUES AS SHOWN IN THE ATTACHED FILE. KINDLY HELP ME WITH CODE TO SEPARATE THE DATA INTO DIFFERENT HOURS SUCH AS 7.30, 8.30, ..., 15.30 AND DIFFERENT MONTHS, JANUARY, FEBRUARY, ... THANKS.
OJO O.S. ojoso@futa.edu.ng
  2 个评论
Scott MacKenzie
Scott MacKenzie 2021-6-19
What do you mean by "separate"? Do you want the data sorted by hour, or sorted by month? Or something else?
Ojo Olusola
Ojo Olusola 2021-6-19
Yes, I want the data sorted by hour and also sorted by month.
That is,
I want the data of each hour sorted separately such as 7.30, 8.30, 9.30, ..., 15.30 across 01-Jan-2000 to 31-Dec-2020. Also, for each time such as 7.30, l want to sort the data of each of the month separately. Thanks.

请先登录,再进行评论。

回答(1 个)

Scott MacKenzie
Scott MacKenzie 2021-6-19
编辑:Scott MacKenzie 2021-6-22
T = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/658625/SAMPLE%20DATA.xlsx');
% combine DATE and TIME columns and replace as single DateTime column
DateTime = T.DATE + T.TIME;
DateTime.Format = 'yyyy-MM-dd HH:mm';
T = [table(DateTime) T(:,3:end)];
% for sorting, create separate columns for year, month, and hour
T.Year = year(T.DateTime);
T.Month = month(T.DateTime);
T.Hour = hour(T.DateTime);
yearColumn = find(string(T.Properties.VariableNames) == "Year");
monthColumn = find(string(T.Properties.VariableNames) == "Month");
hourColumn = find(string(T.Properties.VariableNames) == "Hour");
% sort by month
T1 = sortrows(T, monthColumn);
% sort by hour
T2 = sortrows(T, hourColumn);
% sort by hour, separately within each year
T3 = sortrows(T, [yearColumn hourColumn]);
  4 个评论
Scott MacKenzie
Scott MacKenzie 2021-6-21
Converting to a double array is simple except for the 1st column, since the data in the 1st column are DateTime.
If you just want the numeric data, which start in the 2nd column, then
T{:,2:end}
If you want to include the DateTime information, it must be converted to a serial date number. This will do the trick:
[datenum(T.DateTime) T{:,2:end}]

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by