Separate datasets into months
조회 수: 8 (최근 30일)
이전 댓글 표시
Hello,I have the Data matrix (1X1744) which contains values for two years (2014-2015) and Time (1X1744) is datetime contains the corresponding dates with hours for the values found in Data matrix. My question is: How can I separete the data into months? That is, to have 12 matrices, one for each month in which the valuesfrom each year will be found and another 12 matrices in which there will be the corresponding dates with hours. Your help is important !!!
Below is a piece of code with the data.
[~,~,ALL_Data]=xlsread('Data.xlsx',"Data");
Data=cell2mat(ALL_Data(2:1745,3))'; % 1X1744 douple
Time_Hour_Data=datetime(ALL_Data(2:1745,2),'InputFormat','dd/MM/yyyy HH:mm:ss')'; % 1X1744 datetime
댓글 수: 0
채택된 답변
Rishabh Mishra
2021년 1월 22일
편집: Rishabh Mishra
2021년 1월 22일
Hi,
I understand that you are trying to separate data based on their corresponding datetime values, Each data point should be organized based on which month of the year it belongs to. For that purpose, I have designed a sample code for you to understand how this segregation of data points based on their associated month values can be accomplished.
Consider the code below:
% generate 100 different dates within year 2020
n = 100;
% dateArr stores array of 100 datetime objects in year 2020
for j = 1:n
% datetime(2020,1,1) denotes 1st of january, 2020
% 'randi' is used to add random number of days to above date
dateArr(j) = datetime(2020,1,1) + randi([0 364]);
end
% Segregrate all the dates based on their months
% 1st row of MonthWiseDates contains all the dates that fall in January.
% 2nd row contains all the dates that fall in December.
% Iterate for all the months in an year starting from Jan to Dec
for j = 1:12
% find which datetime objects lie in a particular month
dateInMonth = dateArr(month(dateArr) == j);
% add datetime objects to their corresponding month row
for k = 1:length(dateInMonth)
MonthWiseDates{j,k} = dateInMonth(k);
end
end
I have added appropriate comments at several places for easier understanding.
Hope this helps.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!