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

조회 수: 52 (최근 30일)
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!

채택된 답변

Star Strider
Star Strider 2018년 12월 12일
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!
  댓글 수: 6
Khalil Gabsi
Khalil Gabsi 2018년 12월 12일
Thank you again for your effort! The thing with the boundary is, that it s not really accurate and i also need the data of maxy/miny. I agree it has to do something with the interpolation and increased allt to very high numbers, but it didn´t change a lot. Do you think there´s another method for better interpolation?
Star Strider
Star Strider 2018년 12월 12일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

GT
GT 2018년 12월 11일
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
  댓글 수: 3
GT
GT 2018년 12월 11일
Sorry:( but now I think I understand:
clear all
t = 0:.1:2*pi;
for i = 1:4, y(i,:) = sin(t*2*pi+i);end
plot(t,y)
figure
patch([t fliplr(t)]',[min(y) max(y)]','r')
Khalil Gabsi
Khalil Gabsi 2018년 12월 11일
This would work if the x-values would be for every graph the same, but in my example they start at different x-Points.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by