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

1 回表示 (過去 30 日間)
Ojo Olusola
Ojo Olusola 2021 年 6 月 19 日
編集済み: Scott MacKenzie 2021 年 6 月 22 日
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 件のコメント
Ojo Olusola
Ojo Olusola 2021 年 6 月 21 日
Thank you,the code worked.How can l convert table format to double array?
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 Center および File ExchangeDates and Time についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by