How can I get the maximum line of multiple graphs, starting from different x-values?
51 views (last 30 days)
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:
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 on 12 Dec 2018
This was something of a challenge!
The Code —
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’
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
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!
More Answers (1)
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;
% now to get the max
% now for min
I hope that this helps