Separate datasets into months
5 次查看(过去 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!