how can we find the coordinates of more points besides the points automatically generated by Contour in MATLAB?
Show older comments
I have already known the usage of Contour in MATLAB. NevertheIess, I have an additional question, how can we find the coordinates of more points besides the points automatically generated by Contour? Is interpolation or B-spline fitting available? Looking forward to your reply.
2 Comments
Dyuman Joshi
on 19 Sep 2023
Is there a relation between Z and X,Y values? or is Z independent?
It would help if you can provide more information as to what you are working with.
Yingshi
on 19 Sep 2023
Answers (3)
You can define specific isolines —
[X,Y,Z] = peaks(50);
figure
contourf(X, Y, Z, 'ShowText',1)
title('Default Behaviour')
figure
contourf(X, Y, Z, [-2 -0.5 0 pi 2*pi], 'ShowText',1)
title('Selected Levels')
.
3 Comments
Yingshi
on 19 Sep 2023
Dyuman Joshi
on 19 Sep 2023
@Yingshi, Can you attach the data and the code you are working with?
Star Strider
on 19 Sep 2023
I am not certain what you want to do, or how to interpret ‘uniform discrete sampling points’. If you request it, the first output from contour (or contourf) is a matrix of the contours, explained in the documentation as contour matrix M. You can then get the coordinates from it for each contour level. (This is straightforward but a bit complicated to code.) The matrix exists in two rows, the first row are the x-coordinates of the contour, and the second row are the y-coordinates. This is complicated a bit because the level each contour applies to is the column defined by the level information in the first row and the number of elements in that section of the matrix in the second row:
M = [Level(k) x(1) x(2) ...
Length(k) y(1) y(2) ...
That applies to each level. As I demonstrated, you can define the levels specifically.
You can interpolate the contour matrices for each level to have the same lengths using the interp1 function once you have isolated them in separate cell arrays (since they will have different lengths as contour function outputs).
Bruno Luong
on 19 Sep 2023
Edited: Bruno Luong
on 19 Sep 2023
Not sure if you need more contour (more levels) or denser contour.
For the first case check the doc of contourf the level parameter
For denser points, interpolate te data before call contour
A=peaks(10)
Ai=interp2(A,4,"spline"); % up sampling by 16 = 2^4
x = linspace(0,1,size(A,2));
y = linspace(0,1,size(A,1));
xi = linspace(0,1,size(Ai,2));
yi = linspace(0,1,size(Ai,1));
ax1 = subplot(2,1,1); contourf(x,y,A,0.5+[0 0]); axis equal
ax2 = subplot(2,1,2); contourf(xi,yi,Ai,0.5+[0 0]); axis equal
linkaxes([ax1,ax2])
xlim([0.4 0.7])
ylim([0.4 0.7])
2 Comments
Bruno Luong
on 19 Sep 2023
Edited: Bruno Luong
on 19 Sep 2023
Matt J
on 19 Sep 2023
0 votes
You can interpolate the points uniformly with interparc:
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!

