Day of Week data plot
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Olivia Hakan
le 15 Déc 2023
Commenté : Star Strider
le 21 Déc 2023
Hi, I need to plot daily element concentration averages by day of week (using several months of data). The code I've included does work (I've also included an image of the plot output), but since we measure so many species I'd like to include some kind of for loop to make the process quicker and be able to plot more elements at a time without so much individual specification. Our instrument output gives us concentration and uncertainty columns for each element. Would love any input on this - relatively new to Matlab/coding. Thanks!
Dayofweek = weekday(Xactdata.Date(Data));
Sun = find(Dayofweek == 1); Mon = find(Dayofweek == 2); Tue = find(Dayofweek == 3); Wed = find(Dayofweek == 4); Thu = find(Dayofweek == 5); Fri = find(Dayofweek == 6); Sat = find(Dayofweek == 7);
wk = [1 2 3 4 5 6 7];
S_wk = [Xactdata.SULPHUR(Data)];
S_wk_err = [Xactdata.S_U(Data)];
Cl_wk = [Xactdata.CHLORINE(Data)];
Cl_wk_err = [Xactdata.Cl_U(Data)];
Xact_week_Cl = [mean(Cl_wk(Sun)) mean(Cl_wk(Mon)) mean(Cl_wk(Tue)) mean(Cl_wk(Wed)) mean(Cl_wk(Thu)) mean(Cl_wk(Fri)) mean(Cl_wk(Sat))];
Xact_err_week_Cl = [mean(Cl_wk_err(Sun)) mean(Cl_wk_err(Mon)) mean(Cl_wk_err(Tue)) mean(Cl_wk_err(Wed)) mean(Cl_wk_err(Thu)) mean(Cl_wk_err(Fri)) mean(Cl_wk_err(Sat))];
Xact_week_S = [mean(S_wk(Sun)) mean(S_wk(Mon)) mean(S_wk(Tue)) mean(S_wk(Wed)) mean(S_wk(Thu)) mean(S_wk(Fri)) mean(S_wk(Sat))];
Xact_err_week_S = [mean(S_wk_err(Sun)) mean(S_wk_err(Mon)) mean(S_wk_err(Tue)) mean(S_wk_err(Wed)) mean(S_wk_err(Thu)) mean(S_wk_err(Fri)) mean(S_wk_err(Sat))];
figure
errorbar(wk, Xact_week_S, Xact_err_week_S)
hold on
errorbar(wk, Xact_week_Cl, Xact_err_week_Cl)
xticklabels({'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'})
title('Weekly Concentrations'); %Modify as needed
legend('S', 'Cl')
ylabel('Concentration (ng/m^3)');

0 commentaires
Réponse acceptée
Star Strider
le 15 Déc 2023
Modifié(e) : Star Strider
le 15 Déc 2023
Perhaps something like this —
Date = datetime(2023,1,1, 'Format','yyyy-MM-dd')+caldays(0:365).';
Cl = randn(size(Date)) + 30*sin(2*pi*(0:numel(Date)-1)/6).' + 60;
S = randn(size(Date)) + 60;
F = randn(size(Date)) + 30*cos(2*pi*(0:numel(Date)-1)/6).' + 60;
Data = table(Date,Cl,S,F)
figure
plot(Data.Date, Data{:,2:end})
grid
VN = Data.Properties.VariableNames;
figure
plot(Data.Date, Data{:,2:end})
grid
xlim([datetime('1-Jun-2023') datetime('7-Jun-2023')])
Weekday = weekday(Data.Date);
WeeklyData = accumarray(Weekday, (1:numel(Weekday)).', [], @(x){Data{x,2:end}})
MeanC = cellfun(@mean, WeeklyData, 'Unif',0)
StdeC = cellfun(@(x)std(x)/sqrt(numel(x)), WeeklyData, 'Unif',0)
Mean = cell2mat(MeanC)
Stde = cell2mat(StdeC)
figure
hold on
for k = 1:size(Mean,2)
errorbar((1:7), Mean(:,k), Stde(:,k), 'DisplayName',VN{k+1})
end
hold off
grid
ylabel('Concentration (ng/m^3)')
xticklabels({'Sun','Mon','Tue','Wed','Thr','Fri','Sat'})
legend('Location','best')
It should easily adapt to more columns (variables) in ‘Data’.
.
2 commentaires
Plus de réponses (1)
Image Analyst
le 16 Déc 2023
They will do functions, like mean, based on what group the data is in.
0 commentaires
Voir également
Catégories
En savoir plus sur Annotations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


