MATLAB: How to get the maximum line of multiple graphs, starting from different x-values

areaextractfillMATLABmaximaminimax-values

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!
I already read this answer, but still can´t find a solution for my problem. MathWorks
Thank you in advance!

Best Answer

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 —
How can I get the maximum line of multiple graphs, starting from different x-values - 2018 12 12.png
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!
Related Question