how to plot a certain level in a contour?

10 views (last 30 days)
I 'm using contour option in Matlab R2014a to plot many curves: contour(x,y,c,cilevels); with cilevels = [0.0,.005,0.019];
but I need to select one curve (level) from the contour: the option peaks is not working? and I have tried : contour(x,y,c,[1 1]); but it was error also?
could you please help me?

Accepted Answer

Star Strider
Star Strider on 1 Mar 2025
Extracting the contours for a specific level is straightforward.
Try this —
[x,y,c] = peaks(20);
cilevels = [0.0,.005,0.019];
figure
[c,h] = contour(x,y,c,cilevels);
axlim = axis;
Level = 0.005; % Choose Level
startIdx = find(c(1,:) == Level);
vlen = c(2,startIdx);
for k = 1:numel(startIdx)
% Q = [startIdx(k); vlen(k); startIdx(k)+vlen(k)]
idxrng = startIdx(k)+1 : startIdx(k)+vlen(k);
xc{k} = c(1,idxrng);
yc{k} = c(2,idxrng);
end
figure
hold on
for k = 1:numel(startIdx)
plot(xc{k}, yc{k})
end
hold off
axis(axlim)
.
  16 Comments
Mohamed Hajjaj
Mohamed Hajjaj on 7 Mar 2025
It is geart and I'm grateful to you for all your help and your effort.
Star Strider
Star Strider on 7 Mar 2025
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

Sign in to comment.

More Answers (1)

Cris LaPierre
Cris LaPierre on 3 Mar 2025
I think you want the (x,y) coordinates of your levels. That can be obtained using this syntax: M = contour(___)
However, be sure to read the documentation to understand how the 2xN output array is formatted:
Contour matrix, returned as a two-row matrix of following form.
Z1, x1,1, x1,2, ..., x1,N1, Z2, x2,1, x2,2, ..., x2,N2, Z3, ...
N1, y1,1, y1,2, ..., y1,N1, N2, y2,1, y2,2, ..., y2,N2, N3, ...
The columns of the matrix define the contour lines. Each contour line starts with a column containing Z and N values:
  • 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
Conside this example:
p = peaks(25);
m = contour(p)
m = 2×259
-6.0000 14.4744 14.0000 13.0767 13.4954 14.0000 14.5773 14.4744 -4.0000 15.3227 15.0000 14.0000 13.0000 12.1330 12.0000 11.5885 11.9601 12.0000 13.0000 13.3965 14.0000 15.0000 15.3397 16.0000 16.1878 16.1552 16.0000 15.3227 -2.0000 16.0658 7.0000 6.0000 5.7662 6.0000 7.0000 7.1890 7.0000 6.0000 19.0000 5.0000 4.7869 4.4921 4.5628 5.0000 5.1433 6.0000 7.0000 7.0338 7.8094 8.0000 8.2328 8.1836 8.0000 7.2888 7.0000 6.0000 5.7510 5.0000 27.0000 4.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Inspecting the output m, I see that the first 2 entries correspopnd to the contour levels for -6 and -4. There are 7 (x,y) coordinates designating the -6 contour, and 15 for -4. You won't know the number of points a priori, so you'll probably need a loop to extract the data. Here's one way.
i = 1;
data = [];
while i < length(m)
tmp.clvl = m(1,i);
num = m(2,i);
tmp.x = m(1,i+1:i+num);
tmp.y = m(2,i+1:i+num);
i = i+num+1;
data = [data,tmp];
end
data(1)
ans = struct with fields:
clvl: -6 x: [14.4744 14 13.0767 13.4954 14 14.5773 14.4744] y: [6 5.7662 6 7 7.1890 7 6]
Now that I have that data, I can plot it for comparison.
figure
data(1).clvl
ans = -6
plot(data(1).x,data(1).y)
Note that some contour levels have multiple independent contours (0, for example here). Each will appear as their own item in the output array. Said another way, there are 2 entries for 0 here; entries 5 and 6.
data(5)
ans = struct with fields:
clvl: 0 x: [7.4524 7.1551 7.0405 7.0568 7.2600 8 8.0478 9 9.1791 10 10.8642 11 12 12.5792 13 13.9966 14 15 15.6699 16 17 17.3197 18 18.8752 19 20 20.6058 21 ... ] (1x35 double) y: [1 2 3 4 5 5.8471 6 6.7570 7 7.4351 8 8.0587 8.6493 9 9.3414 10 10.0068 10.5148 10 9.8749 9.1884 9 8.4892 8 7.8475 7.1987 7 6.5074 6 5.9504 5.1578 5 4.2651 4 3.1712]
data(6)
ans = struct with fields:
clvl: 0 x: [1 2 3 3.0329 4 5 5.0995 6 7 7.0792 8 8.7235 9 10 10.0422 10.6609 10.4867 10 9.9630 9.3154 9 8.4278 8 7.3619 7 6.1244 6 5 4.2841 4 3 2 1.7134 1] y: [20.9823 20.6637 20.0768 20 19.6921 19.1473 19 18.6728 18.0895 18 17.5227 17 16.8355 16.0295 16 15 14 13.0895 13 12 11.5847 11 10.4536 10 9.5173 9 ... ] (1x34 double)
So ensure you set a cilevel for the line whose (x,y) data you want.
  5 Comments
Cris LaPierre
Cris LaPierre on 5 Mar 2025
Edited: Cris LaPierre on 7 Mar 2025
The easiest way to view the values is display them on the screen like I did (red box below). Another option is to view the variable in the Variable Editor. Double-click on the variable in the Workspace (red arrow) to open it in the Variable Editor (orange box below)
If a field is too big to show, its size is displayed instead. To see the contents, double click on the hyperlink size in a cell (green arrow above) to open that in the Variable Editor (image below).
If you do want to save to file, there are many options. I would combine them into a table and use writetable to save them to a file.
% select data to save
clvl = data(5).clvl
X = data(5).x;
Y = data(5).y;
% save to a txt file
tbl = table(X,Y);
writetable(tbl,"contour_" + clvl + ".txt")
Mohamed Hajjaj
Mohamed Hajjaj on 7 Mar 2025
It helps me a lot, thank you very much.

Sign in to comment.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Products


Release

R2014a

Community Treasure Hunt

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

Start Hunting!