Interpolating contour plot using user input
显示 更早的评论
Hello,
I have created a 2D contour map using a 25x19 matrix and was wondering how to interpolate the value at certain user-input x-y coordinates? Essentially, I want the user to enter coordinates that are either integer or decimal, and for the code to output the value at that corresponding location. Any help would be appreciated :)

data = readmatrix('pixeltxt.txt');
contourf(data);
grid minor
采纳的回答
I think taking a look at this documentation may help you https://www.mathworks.com/help/matlab/ref/scatteredinterpolant.html
13 个评论
I am a bit confused on the parameters - would v be my 25 x 19 matrix? and if so, do the x and y have to be the same dimensions? Or would I use x and y as my user-inputted coordinates? If you could clarify further, that would be great.
Is it possible to convert my 25x19 matrix using Matlab code (picture attached) to fit a function in terms of z = f(x,y)? That way, I can enter x and y coordinates and the output will be the cooresponding value at that location.

This is exactly what the scatteredinterpolant function does for you. x and y would in this case be the indices of the values in the matrix. To create a vector to input in the function you can use a for loop like this:
A = rand(25,19) %In your case this would be your matrix and not 'rand(25,19)'
x = [];
y = [];
v = [];
for i = 1:size(A,1)
for j = 1:size(A,2)
x = [x; i];
y = [y; j];
v = [v; A(i,j)]
end
end
Now you can create the interpolant function like this:
f = scatteredInterpolant(x,y,v)
and you can read the values like this:
xi = % The x value you want to interpolate at
yi = % The y-value you want to interpolate at
I = f(xi,yi) % The interpolated value
I got an error saying: "
Error using scatteredInterpolant. The input points must be specified in column-vector
format.
Error in pixelexport (line 13)
f = scatteredInterpolant(x,y,v) "
Oh, i see i made a typo. The for loop should be like this:
for i = 1:size(A,1)
for j = 1:size(A,2)
x = [x; i];
y = [y; j];
v = [v; A(i,j)]
end
end
I also corrected my previous comment
That works, thank you so much!!
I do have one final question: Is there a way for me to keep my plot as it is w/ the values, but change the axis? I want the x axis to be -21.4 to -19.6 going up by 0.2. And I want the y axis to be -17.5 to -15 going up by 0.5
I tried set gca but it did not work - it gave me a blank axis overall
Thanks in advance
use:
grid on;
xlim([-21.4 -19.6]);
ylim([-17.5 -15]);
It returned this:

This is exactly what i would expect that it returns. If this is not what you want, i don't understand your question.
I have another scatter plot of nodes on a mesh with the axes I stated previosuly. So, I wanted to modify this contour plot to match that axes so that I can match the position of the nodes to the value in my contour plot. For example, a node positioned at -21, -17 would coorespond to ___ value in the contour plot. I am essentially looking to overlay the contour plot over the scatter plot. I hope this makes more sense
You can use the rescale function:
So for instance you have a vector called A and you want to set the x-axis to [-21.4 -19.6] and y-axis to [-17.5 -15] you can do this:
A(:,1) = rescale(A(:,1),-21.4,-19.6);
A(:,2) = rescale(A(:,2),-17.5,-15.0);
I think this is what you mean
Thanks for your answers Bram Schroeders
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Contour Plots 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
