MATLAB Answers

K E
1

How to extract x and y position of contour line?

K E
さんによって質問されました 2015 年 7 月 23 日
最新アクティビティ Lars K.
さんによって コメントされました 2019 年 2 月 12 日
In old matlab versions, I could extract x and y points along a contour line as shown below. This is incredibly useful in my work. Now in R2015 the approach doesn't work (hh.XData and hh.YData are the x and y values of the data you're contouring, rather than the x,y position of the contour line). In more recent versions of contour, how can I extract x,y points along a contour line? If I can't do it via contour, is there another way?
z = peaks; % Load in a dataset that all matlab users have
x = 1:size(peaks, 2); y = 1:size(peaks, 1);
figure;
pcolor(x, y, z); % Plot the data
shading flat;
hold on;
[cc, hh] = contour('v6', x, y, z, [0 10^5], 'k'); % Overlay contour line (will plot 3 continuous contours)
xPointsOfContour = get(hh(1), 'xdata'); % Get points along the first contour
yPointsOfContour = get(hh(1), 'ydata');
plot(xPointsOfContour, yPointsOfContour, 'w.'); % Demo that points on contour line were extracted

  0 件のコメント

サインイン to comment.

3 件の回答

K E
Answer by K E
on 23 Jul 2015
 Accepted Answer

Just realized that the contour matrix cc now seems to contain x and y location data,
pcolor(x, y, z); % Plot the data
hold on;
shading flat
[cc, hh] = contour(x, y, z, [0 10^5], 'k'); % Overlay contour line
hold on;
cc(cc<1) = NaN;
h = plot(cc(1,:), cc(2, :), 'w.');

  1 件のコメント

But still I have similar problem, I can't access the individual curves that USED to be in contour - children in r2013. What do I have to now manually extract them??

サインイン to comment.


John L
Answer by John L
on 9 Jun 2017

Plot a single contour, then plot(hh.ContourMatrix(1,2:end),hh.ContourMatrix(2,2:end))

  1 件のコメント

hi john,
with your answer you explained something very important using contour. I struggled and lost a lot of time, while i was trying to find intersections of two contour lines.
sectionproblem.png
Plotting both contour matrix as lines and execute
[x,y,~,~]=intersection(contour1(1,:),contour1(2,:),contour2(1,:),contour2(2,:),true)
(btw thanks a lot to Douglas Schwarz) the intersection script found 6 intersections (4 wrong ones). Plotting both contour lines with markers i saw these other data points the first time.
The first column of the contour matrix M contains the value of the contour line and does not belong to the xy data points. With Johns little but important hint using
[x,y,~,~]=intersection(contour1(1,2:end),contour1(2,2:end),contour2(1,2:end),contour2(2,2:end),true)
the problem was solved and i got my two correct intersections.

サインイン to comment.


Answer by Yuhang Luo on 29 Jun 2018

  0 件のコメント

サインイン to comment.



Translated by