# How can I get the maximum line of multiple graphs, starting from different x-values?

51 views (last 30 days)
Khalil Gabsi on 11 Dec 2018
Commented: Star Strider on 12 Dec 2018
Hi everyone! I really don´t know what else to do! I have a lot of graphs from different matrix and on one hand i want to fill the area between the maximum line and the minimum line, on the other i need to extract the data from x- and y-values from these lines. Let me give you an example to make it more clear what i exactly need:
if you have something like that:
hold on;
t =0:0.01:2*pi;
x=sin(t);
hL(1)=plot(t-0.5,x+1);
hL(2)=plot(t+0.5,x+1);
hL(3)=plot(t,x);
hL(4)=plot(t-0.5, x-1);
hL(5)=plot(t+0.5, x-1);
you get this graph: and what i need now is:
getting the point values from the marked lines, and fill the area in between! Star Strider on 12 Dec 2018
This was something of a challenge!
The Code —
hold all
t =0:0.01:2*pi;
x=sin(t);
hL(1)=plot(t-0.5,x+1);
hL(2)=plot(t+0.5,x+1);
hL(3)=plot(t,x);
hL(4)=plot(t-0.5, x-1);
hL(5)=plot(t+0.5, x-1);
Ax = gca;
xt = Ax.XTick; % Get Common Time Vector For All Curves
allt = linspace(min(xt), max(xt), 500); % Common Time Vector
td = get(hL, 'XData');
tdm = cell2mat(td); % Time Data Matrix
yd = get(hL, 'YData'); % Get Curves
ydm = cell2mat(yd); % Curve Data Matrix
newy = zeros(size(tdm,1), numel(allt));
for k1 = 1:size(tdm,1)
newy(k1,:) = interp1(tdm(k1,:), ydm(k1,:), allt); % Map Individual Curve Independent Variables To ‘allt’
end
miny = min(newy); % Minimum Of All Curves
maxy = max(newy); % Maximum Of All Curves
miny = miny(~isnan(miny));
maxy = maxy(~isnan(maxy));
[~,nsc] = find(~isnan(newy),1);
[~,nec] = find(~isnan(newy),1,'last');
patch([allt(nsc:nec), fliplr(allt(nsc:nec))], [miny fliplr(maxy)], [0.7 0.8 0.9], 'EdgeColor','none')
for k1 = 1:numel(hL)
plot(hL(k1).XData, hL(k1).YData) % Re-Plot Original Curves
end
hold off
The Plot — The challenge is to define a common x-vector, then map the curves to it. I used the 'XTick' values to define the limits. I used interp1 to map them.
This appears to be reasonably efficient. I don’t know how robust it would be to other problems, or for different versions of this problem. It seems to work here.
Experiment with it!
Star Strider on 12 Dec 2018
As always, my pleasure!
That interpolation method is the best available. The problem has to do with your data, and there is no other way I can think of to address the problem you have.
There are other Bounding Regions (link) functions to experiment with, alphaShape (link) being one.

GT on 11 Dec 2018
Maybe I miss understood but you could use min and max to achieve this:
t = 0:.1:2*pi;
for i = 1:4, y(i,:) = sin(t*i*2*pi);end;
plot(t,y)
% now to get the max
figure,
plot(t,max(y))
% now for min
figure
plot(t,min(y))
I hope that this helps
Khalil Gabsi on 11 Dec 2018
This would work if the x-values would be for every graph the same, but in my example they start at different x-Points.

R2018b

### Community Treasure Hunt

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

Start Hunting!