Hi,
I understand that you want to calculate the area between 2 contour lines.
‘Contour’ matrix returned by the ‘contour’ function defines contour lines in its columns.
In the following line of your code,
[contour_matrix,h] = contour(a,b,V,[-iso,iso]);
The ‘contour_matrix’ returned by the ‘contour’ function is of the 2x416 dimension.
Contour matrix, returned as a two-row matrix is of the following form
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1490077/image.png)
The columns of the matrix define the contour lines. Each contour line starts with a column containing Z and N values where:
- Zi — The height of the ith contour line
- Ni — The number of vertices in the ith contour line
- (xij, yij) — The coordinates of the vertices for the ith contour line, where j ranges from 1 to Ni
So, the Zi, Ni column pair may appear not only in the first column but also in other columns.
Kindly refer to the following link for more information on ‘contour’ function - https://www.mathworks.com/help/matlab/ref/contour.html#mw_27cd6c94-d861-4e0a-837c-0a19f2574186
The contour_matrix returned by the following line of your code is as follows:
[contour_matrix,h] = contour(a,b,V,[-iso,iso]);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1490082/image.png)
So, to extract the actual coordinates of the contour lines, all the columns like column 1, column 36, column 71 etc., which contain the height and number of vertices of ith contour line must be excluded.
Therefore, to extract the contour coordinates I would recommend changing the following lines of code:
% Extract the contour coordinates
contour_x = contour_matrix(1,2:end);
contour_y = contour_matrix(2,2:end);
I provided correct code below to replace the above lines of code.
% Extract the contour coordinates
currentColumnWithHeight=1;
contour_x = [];
contour_y = [];
while currentColumnWithHeight <= length(contour_matrix)
nextColumnWithHeight=currentColumnWithHeight+contour_matrix(2,currentColumnWithHeight)+1;
x = contour_matrix(1,currentColumnWithHeight+1:nextColumnWithHeight-1);
y = contour_matrix(2,currentColumnWithHeight+1:nextColumnWithHeight-1);
contour_x = [contour_x ,x];
contour_y = [contour_y ,y];
currentColumnWithHeight = nextColumnWithHeight;
end
Hope it helps in resolving your query!