How to combine 2 Matlab figure plots?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Anshuman
am 24 Jul. 2023
Kommentiert: Star Strider
am 24 Jul. 2023
I have created 2 lines on the same plot. I need to take an average of them and add a 3rd line on this figure. Any idea how may I easily do so ? I am attaching the .fig (test_combine.fig) file as well.
Thanks
0 Kommentare
Akzeptierte Antwort
Star Strider
am 24 Jul. 2023
Bearbeitet: Star Strider
am 24 Jul. 2023
Perhaps —
F = openfig('test_combine.fig');
Lines = findobj(F,'Type','line');
for k = 1:numel(Lines)
x{k} = Lines(k).XData;
y{k} = Lines(k).YData;
nlLine(k) = numel(x{k});
end
nlLine
ymtx = [y{1}; y{2}];
aymean = mean(ymtx);
gymean = geomean(ymtx);
plot(x{1}, aymean, '-k', 'DisplayName','Arithmetic Mean')
plot(x{1}, gymean, '--k', 'DisplayName','Geometric Mean')
legend('Location','best')
EDIT — (24 Jul 2023 at 15:51)
For a weighted mean, you would have to supply a (2x1) weighting vector and then multiply each row of ‘ymtx’ (in my code) by the appropriate weight scalar. To weight the elements of each vector, provide a weighting vector and then use element-wise multiplication (.* instead of *).
.
2 Kommentare
Star Strider
am 24 Jul. 2023
Probably something like this —
F = openfig('test_combine.fig');
Lines = findobj(F,'Type','line');
for k = 1:numel(Lines)
x{k} = Lines(k).XData;
y{k} = Lines(k).YData;
nlLine(k) = numel(x{k});
end
% nlLine
ymtx = [y{1}; y{2}];
wv = [1;10]; % Weight Vector (Rows)
ymtx = ymtx .* wv;
aymean = mean(ymtx);
gymean = geomean(ymtx);
plot(x{1}, aymean, '-k', 'DisplayName','Arithmetic Mean')
plot(x{1}, gymean, '--k', 'DisplayName','Geometric Mean')
legend('Location','best')
The ‘wv’ vector weights the first row at 1 and the second row at 10. (The code uses the same matrix for both the arithmetic and geometric mean.)
.
Weitere Antworten (1)
Raheel Naveed
am 24 Jul. 2023
Greetings,
You can take average of data along Y-axis using mean() function.
Assuming your , y-axis data as y1 and y2, we can use below code to create a 3rd graph on same figure
hold on % to plot on same figure
yAvg=mean([y1:y2]) % You can replace your variables here
In case, there's still any problem, share the whole code for better debugging (or .mat file )
2 Kommentare
Siehe auch
Kategorien
Mehr zu Line Plots 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!