How to combine 2 Matlab figure plots?

4 次查看(过去 30 天)
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

采纳的回答

Star Strider
Star Strider 2023-7-24
编辑:Star Strider 2023-7-24
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
nlLine = 1×2
4000 4000
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 个评论
Anshuman
Anshuman 2023-7-24
Can you show the code for weighted mean? Thanks
Star Strider
Star Strider 2023-7-24
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
nlLine = 1×2
4000 4000
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.)
.

请先登录,再进行评论。

更多回答(1 个)

Raheel Naveed
Raheel Naveed 2023-7-24
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 个评论
Anshuman
Anshuman 2023-7-24
It says Unrecognized function or variable 'y1'. How will I assign plots to variables?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Line Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by