How to extract x and y position of contour line?

조회 수: 167 (최근 30일)
K E
K E 2015년 7월 23일
댓글: KFoster 2021년 10월 1일
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

채택된 답변

K E
K E 2015년 7월 23일
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.');
  댓글 수: 8
Ayush Tiwari
Ayush Tiwari 2021년 7월 29일
what does 'w.' do here ?
Adam Danz
Adam Danz 2021년 7월 29일
w = white
'.' = point markers
w. = white point markers

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

추가 답변 (2개)

Yuhang Luo
Yuhang Luo 2018년 6월 29일
  댓글 수: 1
Adam Danz
Adam Danz 2020년 1월 23일
Also see
[cm, h] = contour(. . .);
contourTable = getContourLineCoordinates(cm);
% --or--
contourTable = getContourLineCoordinates(h);
% contourTable =
% Level Group X Y
% ________ _____ __________ __________
% -5.8504 1 -0.040982 -1.625
% -5.8504 1 -0.027916 -1.75
% -5.8504 1 0 -1.7823
% -5.8504 1 0.045032 -1.5
% etc....

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


John L
John L 2017년 6월 9일
Plot a single contour, then plot(hh.ContourMatrix(1,2:end),hh.ContourMatrix(2,2:end))
  댓글 수: 2
Lars K.
Lars K. 2019년 2월 12일
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.
KFoster
KFoster 2021년 10월 1일
The first column of the contour matrix M denotes what contour level and how many vertices are included in that particular line; similar columns are found throughout the M matrix between the coordinates for each line. It's in the matlab documentation here: https://www.mathworks.com/help/matlab/ref/contour.html#mw_6bc1813f-47cf-4c44-a454-f937ea210ab6

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

카테고리

Help CenterFile Exchange에서 Contour Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by