version 1.0.0.0 (1.47 KB) by
Michael Kleder

For MATLAB 7 or later. Returns actual coordinates of polygons representing contour lines ....

1 Download

Updated 09 Sep 2005

No License

If you've used the CONTOUR function, you know how easy and fast it is, but you also know that it's difficult to extract the actual polygon coordinates of the contour lines in your data. Such coordinates are needed for any calculation beyond simple contour plotting. Two examples would be (1) determining whether a particular point is within a contour line, and (2) determining the area of a region inside a contour line. This function provides that capability in a user-friendly way.

GETCON - compute coordinates of contour lines (Matlab 7 or later)

[xc,yc] = contour(X,Y,Z,v)

X,Y,Z = coordinates of a function Z=f(X,Y), where X and Y are matrices of the sort generated by meshgrid or ndgrid and Z is the value of the function of X and Y at each corresponding matrix element

v = one or optionally, more than one, contour altitude(s)

If v is a scalar:

[xc,yc] = Column vectors containing the NaN-delimited polygon(s) defining the contour slice at altitude v

If v is a vector:

[xc,yc] = Cell arrays of column vectors. Each entry cell array entry contains the NaN-delimited polygon(s) defining the contour slice at the altitude in the corresponding entry in v. For example, if v is a vector with three entries, then xc and yc will be cell arrays, each with three entries. The first cell array entry in xc and the first cell array entry in yc will each contain a vector. Those vectors are the x and y coordinates of the NaN-delimited polygons defining the contour slice at the altitude specified by the first entry in v.

Notes:

(1) Version 1.0, initial writing, Michael Kleder, Sep 2005

(2) This application is VERY SENSITIVE TO MATLAB VERSION. This function is written for MATLAB 7.

(3) No warranty. Use at your own risk.

Michael Kleder (2021). Slicing 3-D data: Obtaining Coordinates of Contour Polygons (https://www.mathworks.com/matlabcentral/fileexchange/8453-slicing-3-d-data-obtaining-coordinates-of-contour-polygons), MATLAB Central File Exchange. Retrieved .

Created with
R14SP2

Compatible with any release

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

Start Hunting!Create scripts with code, output, and formatted text in a single executable document.

Adam DanzAlso see this solution to extract coordinates to a known contour level [1] and this file exchange function to extract all coordinates [2].

[1] https://www.mathworks.com/matlabcentral/answers/501139-how-to-get-coordinates-for-a-specific-contour-line

[2] https://www.mathworks.com/matlabcentral/fileexchange/74010-getcontourlinecoordinates

RikSince the change in graphics handling in R2014b, this code will not work anymore.

Star Strider (https://www.mathworks.com/matlabcentral/answers/336169-how-to-extract-x-y-z-coordinates-from-a-contour#answer_263666) has written some code, which I enveloped here. So to solve the version dependency for the most part, replace these lines

[c,h]=contour(X,Y,Z,v);

xcg=get(get(h,'children'),'xdata');

ycg=get(get(h,'children'),'ydata');

with these lines

[c,h]=contour(X,Y,Z,v);

%get the Matlab version, add 0.5 to the year for the b release

MatlabVersion=version('-release');

MatlabVersion=str2double(MatlabVersion(1:(end-1)))...

+0.5*strcmp(MatlabVersion(end),'b');

if MatlabVersion<=2014%older versions

xcg=get(get(h,'children'),'xdata');

ycg=get(get(h,'children'),'ydata');

else%newer versions

Lvls = h.LevelList;

conturc=cell(length(Lvls),2);%pre-allocate variable

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

conturc=conturc(:);

for n=1:length(conturc)

conturc{n}=[conturc{n} NaN(2,1)];%add a NaN column

end

xcg=conturc;%this is a copy, containing both x and y

ycg=conturc;%this is a copy, containing both x and y

for n=1:length(xcg)

xcg{n}=xcg{n}(1,:)';%remove y

ycg{n}=ycg{n}(2,:)';%remove x

end

end

MCCHi Michael,

I have tried to your code in MATLAB 2016a. It gives me error, which is "Subscript indices must either be real positive integers or logicals". I think your code doesn't work in this version. I have been struggled with problem for several days. Since your code doesn't have comments, it's difficult to find out what the problem is. I am wandering can you give me some suggestions on how to resolve this problem.

Thanks

MCCMichael HeistandI have a need to convert contour lines to polygons, so I was very excited to find this function. Unfortunately, it does not work in MATLAB 2015a. With the new graphics engine, the contour object no longer has children. Any other ideas?

Rashbeardnice file, works well

Luke OwensVery easy to use.

Alexi Ivanov