How to plot the highest y values for each x value for a figure with multiple plots

2 views (last 30 days)
Edema Rhu on 20 Jul 2018
Commented: gwoo on 26 Jul 2018
I have a figure with 9 graphs plotted in it. Each line was plotted in one iteration of a loop, so, as I run my code, I don't conserve x nor y values. Instead, new ones are generated. Is it possible to plot a single line that has the highest y value for each x value? Here's an example of the type of graph I have in mind
0 CommentsShowHide -1 older comments

Sign in to comment.

Answers (1)

Fangjun Jiang on 20 Jul 2018
I would recommend you saving the data for each line when it is created running the loop. If not possible, you could get the data from the figure by going through its children and the 'XData' and 'YData'. Here is an example
f=figure;hold on;
plot(rand(10,3))
a=f.Children;
L=a.Children;
D={L.YData};
E=cell2mat(D');
plot(L(1).XData,max(E),'r')
1 CommentShowHide None
gwoo on 26 Jul 2018
How would you do this if they weren't on the same X domain? Notice the following doesn't give so nice an answer (I'm using minimum instead of maximum):
f=figure;hold on;
plot(sort(rand(10,1)),rand(10,1),'b',sort(rand(10,1)),rand(10,1),'g',sort(rand(10,1)),rand(10,1),'k')
a=f.Children;
L=a.Children;
D={L.YData};
E=cell2mat(D');
plot(L(1).XData,min(E),'r'

Sign in to comment.

R2017b

Community Treasure Hunt

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

Start Hunting!