Output argument of Matlab contour function
Show older comments
Dear all,
The "M = contour___" Matlab function returns the contour matrix M, which contains the x, y coordinates of the vertices at each level. For a given level, what is the ordering of the points returned by this function?
A simple script with the scatter function to visualize the output allows concluding that the ordering follows the natural profile of the curve but what is the mathematical algorithm involved? I do thank you for your attention.
Many thanks. Carole
2 Comments
"... what is the mathematical algorithm involved?"
As I recall, the algorithm used to be explained on some tech doc... but these are long gone.
But luckily for you I know that it was saved in the Internet Archive (scroll down to "The Contouring Algorithm"):
"For a given level, what is the ordering of the points returned by this function?"
The output matrix is explained here:
It is a very unhelpful output format.
You might like to use a wrapper function which returns a more useful and clearer output by downloading one of these:
Adam Danz
on 24 Jan 2024
Currently the contour functions use a marching squares agorithm which changed on or around R2014b.
Another file exchange function that may come in handy is getContourLineCoordinates which returns a table of coordinates for all contour lines and some grouping variables to identify level and line groups. I believe the coordinates are listed in the table in a circular order.
Answers (1)
" For a given level, what is the ordering of the points returned by this function? "
The contour matrix stored in the first output of contour() as well as the in the second output (h.ContourMatrix) is produced within a restricted p-file contour.p (matlab.graphics.chart.primitive.Contour) which prevents us from opening it and determining the algorithm that controls the order of coordinates for each level.
However, you can poke around with outputs to determine the order after plotting.
The contour matrix is [2 x n] and the top row contains x coordinates while the bottom row contains y coordinates. In addition to that, the level and the number of elements for that level are included for each contour line. This is all explained in the documentation. For example, if the first few columns of the matrix are
m(:,1:5)
ans =
-1.5 -1.2461 -1.3328 -1.4597 -1.5867
67 2.1579 2.1281 2.1027 2.0957
that tells us that the first contour line listed has a level of -1.5 and there are 67 coordinates associated with that contour line. The next 67 columns defines those coordinates (columns 2:68) and then you'll have another [level;count] pair at column number 69. To list all unique levels: h.LevelList.
The C2xyz() FEX file converts this matrix into a [m x 3] matrix where the x and y coordinates are stored in the first and second column while the level is indicated in the third column. This format is much more useful and easier to work with.
Note that the second output to contour() also contains the X,Y,Z coordinates of your data but these are not listed in the same order as the controur matrix. (h.XData, h.YData, h.ZData).
Categories
Find more on Contour Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!