How to extract x,y,z coordinates from a contour?

31 ビュー (過去 30 日間)
MCC
MCC 2017 年 4 月 19 日
編集済み: Adam Danz 2020 年 2 月 11 日
Hi everybody,
I used the following codes to extract X,Y,Z coordinates from a contour. When I ran it in MATLAB 2013a, it works. But when I ran it in MATLAB 2016a, it doesn't work. I found that the codes is very sensitive to MATLAB version. Can anybody tell me how to solve this problem? Or are there any other MATLAB codes that serve the similar function? The code is from this link:
  2 件のコメント
Adam Danz
Adam Danz 2020 年 1 月 23 日
編集済み: Adam Danz 2020 年 2 月 11 日
In addition to the answers below, see this file exchange function that returns a table of (x,y) coordinates of the contour lines.
MCC
MCC 2020 年 2 月 11 日
Great. Thank you.

サインインしてコメントする。

採用された回答

Star Strider
Star Strider 2017 年 4 月 19 日
This works in R2017a, so it should also work for R2016a.
Example
f = @(x,y) x.^2 - y.^2; % Your Function
x = linspace(-10, 10); % X-Range
y = linspace(-10, 10); % Y-Range
[X,Y] = meshgrid(x,y); % Create Matrix Arguments
figure(1)
[C,h] = contour(X, Y, f(X,Y));
grid
Lvls = h.LevelList;
for k1 = 1:length(Lvls)
idxc{k1} = find(C(1,:) == Lvls(k1));
Llen{k1} = C(2,idxc{k1});
conturc{k1,1} = C(:,idxc{k1}(1)+1 : idxc{k1}(1)+1+Llen{k1}(1)-1);
conturc{k1,2} = C(:,idxc{k1}(2)+1 : idxc{k1}(2)+1+Llen{k1}(2)-1);
end
figure(2)
plot(conturc{3,1}(1,:), conturc{3,1}(2,:))
grid
The contour data now are a (2xN) vector created by horizontally concatenating the (x,y) coordinates of each contour line. The segments are created each as:
C = [Level x-coordinates; Length y-coordinates]
so the first column of each segment are the ‘Level’ and the ‘Length’ of the segment. The second output are the properties of the plot.
My code first plots the contour function, returning both outputs. It then uses the ‘LevelList’ property to return the plotted levels, and then uses these in the loop to find the start indices of the segments in the ‘C’ matrix in the ‘idxc’ cell array, and the number of elements in each contour segment in the ‘Llen’ cell array. It then uses these to extract the x (first row) and y (second row) coordinates for each contour segment in the ‘conturc’ cell array. The z-coordinates are the levels. They do not appear in the cell arrays, but correspond to the values in the ‘Lvls’ vector.
The plot in figure(2) is not necessary for the code. It shows how to get the data from the cell arrays (in this instance the third cell in the first group), and that the extracted values are correct.
This turned out to be an interesting adventure!
  4 件のコメント
MCC
MCC 2017 年 4 月 20 日
Thank you very much.
Star Strider
Star Strider 2017 年 4 月 20 日
As always, my pleasure!

サインインしてコメントする。

その他の回答 (1 件)

Rik
Rik 2017 年 4 月 19 日
The culprit is in this code snippet:
[c,h]=contour(X,Y,Z,v);
xcg=get(get(h,'children'),'xdata');
ycg=get(get(h,'children'),'ydata');
The form in which h is returned is slightly different between 2013a and 2016a. The first returns a handle, the second an object. Usually this doesn't matter, but it does in this case.
I compared the results from [c,h]=countour(flipud(P)) between 2012b and 2017a (after load penny;). Apparently, the newer object no longer has a child for each contour element, so you will have to put in some effort to add in the NaNs yourself to get back the original behavior.
  5 件のコメント
MCC
MCC 2017 年 4 月 20 日
Thank you.
Rik
Rik 2017 年 4 月 20 日
You're very much welcome.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeContour Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by