How to combine 2 Matlab figure plots?

1 visualización (últimos 30 días)
Anshuman
Anshuman el 24 de Jul. de 2023
Comentada: Star Strider el 24 de Jul. de 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

Respuesta aceptada

Star Strider
Star Strider el 24 de Jul. de 2023
Editada: Star Strider el 24 de Jul. de 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
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 comentarios
Anshuman
Anshuman el 24 de Jul. de 2023
Can you show the code for weighted mean? Thanks
Star Strider
Star Strider el 24 de Jul. de 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
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.)
.

Iniciar sesión para comentar.

Más respuestas (1)

Raheel Naveed
Raheel Naveed el 24 de Jul. de 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 comentarios
Anshuman
Anshuman el 24 de Jul. de 2023
What about weighted mean?
Anshuman
Anshuman el 24 de Jul. de 2023
It says Unrecognized function or variable 'y1'. How will I assign plots to variables?

Iniciar sesión para comentar.

Categorías

Más información sobre Line Plots en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by