Effacer les filtres
Effacer les filtres

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

19 vues (au cours des 30 derniers jours)
MCC
MCC le 19 Avr 2017
Modifié(e) : Adam Danz le 11 Fév 2020
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 commentaires
Adam Danz
Adam Danz le 23 Jan 2020
Modifié(e) : Adam Danz le 11 Fév 2020
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 le 11 Fév 2020
Great. Thank you.

Connectez-vous pour commenter.

Réponse acceptée

Star Strider
Star Strider le 19 Avr 2017
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 commentaires
MCC
MCC le 20 Avr 2017
Thank you very much.
Star Strider
Star Strider le 20 Avr 2017
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (1)

Rik
Rik le 19 Avr 2017
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 commentaires
MCC
MCC le 20 Avr 2017
Thank you.
Rik
Rik le 20 Avr 2017
You're very much welcome.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Contour Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by