Separate datasets into months
3 views (last 30 days)
Show older comments
stelios loizidis
on 19 Jan 2021
Commented: stelios loizidis
on 26 Jan 2021
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 Comments
Accepted Answer
Rishabh Mishra
on 22 Jan 2021
Edited: Rishabh Mishra
on 22 Jan 2021
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.
More Answers (0)
See Also
Categories
Find more on Dates and Time in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!