Plotting several plots of respective ranges from an excel file into one single plot
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Sergio
am 19 Feb. 2024
Kommentiert: Dyuman Joshi
am 19 Feb. 2024
Hello, I have this excel file which contains recordings over several years of the temperature at an airport. When I inspect it , I see it has all the years from 1961 to 1979.
I would like to extract all the Juli months from the file and send them to an own matrix file with the respective dates.
The variables are Var3 and Var4 for the columns. However, each July month is given for several intervals. The first interval is given for July 1961, which is in the rows 191-222. The the next year comes, with July defined on rows 556-586, etc for the next 18 years.
How can I extract only the July months from this excel into a matrix, called D, and then plot these over each other with different colors and calculate their average? I tried
opts = detectImportOptions('tempDataTrollhFlygpl.xlsx', ...
'Range', '191:222', 'VariableNamingRule','preserve');
opts = setvartype(opts, 4, 'double');
D=readtable('tempDataTrollhFlygpl.xlsx', opts);
M = D(:,[3 4])
% plot(M.Var3,(M.Var4))
xlabel('Date');
ylabel('Temperature');
A = mean(M, 4)
plot((M.Var3),(M.Var4))
However, this is only for the first year, and the mean does not give any answer. How can this be done?
Thanks
0 Kommentare
Akzeptierte Antwort
Star Strider
am 19 Feb. 2024
Bearbeitet: Star Strider
am 19 Feb. 2024
Perhaps this —
opts = detectImportOptions('tempDataTrollhFlygpl.xlsx', 'VariableNamingRule','preserve', 'HeaderLines',9);
opts = setvartype(opts, 4, 'double');
D=readtable('tempDataTrollhFlygpl.xlsx', opts);
M = D(:,[3 4])
Lv = month(M{:,1}) == 7;
M7 = M(Lv,:)
D = unique(day(M7{:,1}));
Y = year(M7{:,1});
Yu = unique(Y);
Yidx = Y-Y(1)+1;
Year_7c = accumarray(Yidx, (1:numel(Yidx)).', [], @(x){M7{x,2}}); % July For Each Year As Separate Cell Element
Year_7m = cat(2,Year_7c{:}); % July Matrix (Columns = Years Corresponding To The 'Y' Vector)
A = mean(cat(2,Year_7c{:}),2);
Asem = std(cat(2,Year_7c{:}),[],2)/sqrt(numel(Year_7c));
cv = tinv([0.025 0.975], numel(Year_7c)-1);
hold on
for k = 1:numel(Year_7c)
hp(k) = plot(D,Year_7c{k}, 'DisplayName',string(Yu(k)));
end
hpm = plot(D, A, '-k', 'LineWidth',2, 'DisplayName','Mean');
hpci = plot(D,Asem*cv+A, '--k', 'LineWidth',2, 'DisplayName','95% CI');
hold off
xlabel('Date');
ylabel('Temperature');
title('Juli')
legend([hp hpm hpci(1)], 'Location','eastoutside')
% A = mean(M, 4)
% plot((M.Var3),(M.Var4))
The code first identifies the July entries and the corresponding years. It then uses accumarray to aggregate the days in every July as elements of a cell array, converts them to a numeric matrix, and then plots them as a function of the days. It also calculates the daily mean, and 95% confidence intervals based on the critical values of the t-distribution.
EDIT — Corrected typographical errors.
.
0 Kommentare
Weitere Antworten (1)
Dyuman Joshi
am 19 Feb. 2024
Bearbeitet: Dyuman Joshi
am 19 Feb. 2024
opts = detectImportOptions('tempDataTrollhFlygpl.xlsx', ...
'Range', 'A10', 'VariableNamingRule','preserve');
opts = setvartype(opts, 4, 'double');
D=readtable('tempDataTrollhFlygpl.xlsx', opts);
%get the year and month of all the dates
[y, m, d] = ymd(D{:,3});
%Data corresponding to the month of July
idx = m==7;
yrs = unique(y).';
n = numel(yrs);
avg = zeros(n,1);
figure
hold on
for k=1:numel(yrs)
index = (y==yrs(k) & idx);
plot(d(index), D{index,4}, 'DisplayName', string(yrs(k)))
avg(k) = mean(D{index,4});
end
xlabel('Date');
ylabel('Temperature');
hold off
legend('Location', 'EastOutside')
avg
1 Kommentar
Dyuman Joshi
am 19 Feb. 2024
I mixed up a few things in hurry, which I have cleaned up now.
This should be much more clear now.
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!