Day of Week data plot

10 views (last 30 days)
Olivia Hakan
Olivia Hakan on 15 Dec 2023
Commented: Star Strider on 21 Dec 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)');

Accepted Answer

Star Strider
Star Strider on 15 Dec 2023
Edited: Star Strider on 15 Dec 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)
Data = 366×4 table
Date Cl S F __________ ______ ______ ______ 2023-01-01 60.404 59.843 90.561 2023-01-02 84.582 59.642 74.518 2023-01-03 86.675 57.159 44.085 2023-01-04 61.076 59.3 31.712 2023-01-05 32.1 61.74 44.652 2023-01-06 35.14 59.217 76.156 2023-01-07 57.455 59.461 92.111 2023-01-08 86.35 61.043 76.228 2023-01-09 85.904 60.479 45.534 2023-01-10 60.87 60.461 29.131 2023-01-11 31.983 60.05 44.485 2023-01-12 34.402 60.3 74.91 2023-01-13 60.478 60.255 89.784 2023-01-14 86.566 59.59 74.885 2023-01-15 87.031 60.608 43.384 2023-01-16 60.166 59.714 31.5
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}})
WeeklyData = 7×1 cell array
{53×3 double} {53×3 double} {52×3 double} {52×3 double} {52×3 double} {52×3 double} {52×3 double}
MeanC = cellfun(@mean, WeeklyData, 'Unif',0)
MeanC = 7×1 cell array
{[60.4105 59.9299 59.8582]} {[60.0404 60.1430 59.4974]} {[59.4016 60.0599 59.1757]} {[59.1616 60.0115 60.1483]} {[59.4423 59.8680 60.7275]} {[60.4756 59.6932 60.8323]} {[60.8717 60.0839 59.9478]}
StdeC = cellfun(@(x)std(x)/sqrt(numel(x)), WeeklyData, 'Unif',0)
StdeC = 7×1 cell array
{[1.6846 0.0794 1.7129]} {[1.7274 0.0738 1.6822]} {[1.7232 0.0850 1.6994]} {[1.6920 0.0929 1.7300]} {[1.7262 0.0821 1.7206]} {[1.7278 0.0714 1.7012]} {[1.7138 0.0814 1.7216]}
Mean = cell2mat(MeanC)
Mean = 7×3
60.4105 59.9299 59.8582 60.0404 60.1430 59.4974 59.4016 60.0599 59.1757 59.1616 60.0115 60.1483 59.4423 59.8680 60.7275 60.4756 59.6932 60.8323 60.8717 60.0839 59.9478
Stde = cell2mat(StdeC)
Stde = 7×3
1.6846 0.0794 1.7129 1.7274 0.0738 1.6822 1.7232 0.0850 1.6994 1.6920 0.0929 1.7300 1.7262 0.0821 1.7206 1.7278 0.0714 1.7012 1.7138 0.0814 1.7216
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 Comments
Olivia Hakan
Olivia Hakan on 21 Dec 2023
this worked, thanks so much!
Star Strider
Star Strider on 21 Dec 2023
As always, my pleasure!

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 16 Dec 2023
They will do functions, like mean, based on what group the data is in.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!