Clear Filters
Clear Filters

Separate datasets into months

6 views (last 30 days)
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.
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

Accepted Answer

Rishabh Mishra
Rishabh Mishra on 22 Jan 2021
Edited: Rishabh Mishra on 22 Jan 2021
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]);
% 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);
I have added appropriate comments at several places for easier understanding.
Hope this helps.
  1 Comment
stelios loizidis
stelios loizidis on 26 Jan 2021
Perfect. It works!!! Thanks for the valuable help.

Sign in to comment.

More Answers (0)


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!