How to combine 2 Matlab figure plots?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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
0 comentarios
Respuesta aceptada
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
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
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
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.)
.
Más respuestas (1)
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
Ver también
Categorías
Más información sobre Line Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!