differentiate between month in dataset.
Afficher commentaires plus anciens
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?
Réponses (2)
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
1 commentaire
Mathieu NOE
le 6 Avr 2023
hello
problem solved ?
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)]
Data = randn(secondsperyear,1);
T1 = table(DT, Data);
TT1 = table2timetable(T1)
HCounts = retime(TT1, 'monthly', 'count')
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.
.
Catégories
En savoir plus sur Timetables dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

