differentiate between month in dataset.

3 views (last 30 days)
laura lomholt
laura lomholt on 29 Mar 2023
Commented: Mathieu NOE on 6 Apr 2023
Hi, I have multiple data output from simulink, which i have saved in matlab as .mat, but this data is for a whole year in seconds but i want to differentiate between months. so my data set is just a datapoint each second for a whole, year and i want to be able to divide it into january, Feburary, ect... so that i can make a barchart that compare each month. I tried using findgoups, but then i need to create a collumn in the data set where the frist 31*24*3600 seconds is number 1 for january, and then the next 29*24*3600 seconds is number 2 for february. Do anybody know a smart way to do this?

Answers (2)

Mathieu NOE
Mathieu NOE on 29 Mar 2023
hello
maybe this ?
% simplified code for one year scalar input
years = 2023; %
mm = (1:12); % monthes
days_per_month = datenum(years, mm(:)+1, 1) - datenum(years, mm(:), 1); % adding 1 to the month
seconds_per_month = 24*60*60*days_per_month;
% dummy data example
% one full year of data should have generated 31536000 samples , that can
% be now splited in 12 separate chuncks
samples = sum(seconds_per_month);
data_one_year = (1:samples) + rand(1,samples);
% do some math like averaging each month data
for ci = 1:12
duration = seconds_per_month(ci);
if ci == 1
start = 1;
else
start = stop +1;
end
stop = start + duration -1;
data_extract(ci) = mean(data_one_year(start:stop));
end
name = monthName(mm);
plot(mm,data_extract,'*-')
set(gca,'XTick',mm);
set(gca,'XTickLabel',name);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function name = monthName(num)
name = month(datetime(1,num,1), 'name');
end

Star Strider
Star Strider on 29 Mar 2023
Perhaps something like this —
secondsperyear = 60*60*24*365;
DT = datetime(2023,1,1,0,0,0) + seconds(0:secondsperyear-1).';
Ends = [DT(1); DT(end)]
Ends = 2×1 datetime array
01-Jan-2023 00:00:00 31-Dec-2023 23:59:59
Data = randn(secondsperyear,1);
T1 = table(DT, Data);
TT1 = table2timetable(T1)
TT1 = 31536000×1 timetable
DT Data ____________________ ________ 01-Jan-2023 00:00:00 -0.10644 01-Jan-2023 00:00:01 0.25004 01-Jan-2023 00:00:02 -0.30974 01-Jan-2023 00:00:03 -0.38354 01-Jan-2023 00:00:04 0.94189 01-Jan-2023 00:00:05 0.76489 01-Jan-2023 00:00:06 0.98377 01-Jan-2023 00:00:07 0.51009 01-Jan-2023 00:00:08 -2.4735 01-Jan-2023 00:00:09 -1.0738 01-Jan-2023 00:00:10 -0.1024 01-Jan-2023 00:00:11 -0.64083 01-Jan-2023 00:00:12 -1.2975 01-Jan-2023 00:00:13 2.0611 01-Jan-2023 00:00:14 -0.4892 01-Jan-2023 00:00:15 0.26055
HCounts = retime(TT1, 'monthly', 'count')
HCounts = 12×1 timetable
DT Data ___________ __________ 01-Jan-2023 2.6784e+06 01-Feb-2023 2.4192e+06 01-Mar-2023 2.6784e+06 01-Apr-2023 2.592e+06 01-May-2023 2.6784e+06 01-Jun-2023 2.592e+06 01-Jul-2023 2.6784e+06 01-Aug-2023 2.6784e+06 01-Sep-2023 2.592e+06 01-Oct-2023 2.6784e+06 01-Nov-2023 2.592e+06 01-Dec-2023 2.6784e+06
figure
bar(HCounts.DT,HCounts.Data)
xtickformat('MMM')
xlabel('Months')
ylabel('Counts')
The retime function has several aggregation and calculation options. Counting the number of occurrences in each month is only one of them.
.

Categories

Find more on Language Fundamentals 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!