How can I get the maximum line of multiple graphs, starting from different x-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!

 Respuesta aceptada

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 comentarios

I love you! Thank you so much!!!
As always, my pleasure!
Maybe you have any idea, why I get for other Data with basically the same structure, this kind of filling. untitled.jpg
Please note that they do not have ‘basically the same structure’. The data in your Question have monotonically-increasing independent variables. The curves in the most recent figure do not appear to. I suspect that is confusing interp1.
One possibility is to increase the resolution (length) of ‘allt’ to 5000 or more.
Also consider using the boundary (link) function. Use ‘allt’, ‘miny’, and ‘maxy’ to define the polygon.
For example (referring to my previous code):
xv = [allt(nsc:nec), fliplr(allt(nsc:nec))]';
yv = [miny, fliplr(maxy)]';
k = boundary(xv, yv, 0.99);
figure
plot(xv(k)', yv(k)')
It does not work well with the code you posted, even with a very long ‘allt’ vector, because of the abrupt transitions. It could work with the data you really want to use it with.
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?
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.

Iniciar sesión para comentar.

Más respuestas (1)

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 comentarios

Thanks for your answer, but that´s not what i needed!
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')
This would work if the x-values would be for every graph the same, but in my example they start at different x-Points.

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Productos

Versión

R2018b

Preguntada:

el 11 de Dic. de 2018

Comentada:

el 12 de Dic. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by