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

조회 수: 2 (최근 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 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