How do I calculate area enclosed by contour lines?

100 visualizaciones (últimos 30 días)
Kushagra Saxena
Kushagra Saxena el 3 de Jun. de 2014
Comentada: S Ch el 2 de Ag. de 2021
I have
[x, y, z] = peaks; [C, h] = contour(x, y, z, 20);
Kindly suggest an algorithm or necessary functions to find the area enclosed by, and between, contour lines.
Thanks!

Respuesta aceptada

Sara
Sara el 3 de Jun. de 2014
[x, y, z] = peaks;
[C, h] = contour(x, y, z, 20);
ch = get(h,'children');
area = zeros(numel(ch),1);
for i = 1:numel(ch)
x = get(ch(i),'xdata');
y = get(ch(i),'ydata');
[x,y] = poly2cw(x,y);
area(i) = polyarea(x,y);
end
  3 comentarios
Sara
Sara el 3 de Jun. de 2014
It's in the mapping toolbox. It orders the points in a contour in clockwise order, so that the contour is external and an area can be computed (counterclockwise contours are considered internal contours). This is ArcGIS convention and it's followed also by Matlab (not sure who started first). I personally don't have a version working with the basic matlab license and never looked if there is something similar in the file exchange since I have the mapping toolbox.
XING ZHENG
XING ZHENG el 7 de Nov. de 2017
Dear Sara,
Now I am using your function in the version 2017b: [C, h] = contour(x, y, z, 20); ch = get(h,'children');
However, I can not get 'h' from this sentence. therefore, there is No result of ch. I think this is the difference between the version 2014b and 2017b.
How can I fix this problem? Thanks
Xing

Iniciar sesión para comentar.

Más respuestas (1)

A. Shokouhi
A. Shokouhi el 19 de Sept. de 2018
I believe some commands in the code provided by Sara is not available in new versions here a code I wrote down that does the same job.
[x, y, z] = peaks;
[C, h] = contour(x, y, z, 5);
n = 0;
i = 1;
sz = size(h.ContourMatrix,2);
nn(1) = h.ContourMatrix(2,1);
xx = h.ContourMatrix(1,2:nn(1)+1);
yy = h.ContourMatrix(2,2:nn(1)+1);
area(1) = polyarea(xx,yy);
while n+nn(i)+i < sz
n = n + nn(i);
i = i + 1;
nn(i)=h.ContourMatrix(2,n+i);
xx = h.ContourMatrix(1,n+i+1:n+nn(i)+i);
yy = h.ContourMatrix(2,n+i+1:n+nn(i)+i);
area(i) = polyarea(xx,yy);
end
  4 comentarios
Sesha Sai Anudeep Karnam
Sesha Sai Anudeep Karnam el 20 de Mzo. de 2020
Is this area enclosed by contour lines or area enclosed between contour lines?
S Ch
S Ch el 2 de Ag. de 2021
@Sesha Sai Anudeep Karnam Hello, did you have an answer ? I think it gives the area of the colors,
Is it the integral ?

Iniciar sesión para comentar.

Categorías

Más información sobre Contour Plots en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by