フィルターのクリア

How to combine 2 Matlab figure plots?

4 ビュー (過去 30 日間)
Anshuman
Anshuman 2023 年 7 月 24 日
コメント済み: Star Strider 2023 年 7 月 24 日
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 日
What about weighted mean?
Anshuman
Anshuman 2023 年 7 月 24 日
It says Unrecognized function or variable 'y1'. How will I assign plots to variables?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by