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

 채택된 답변

Bram Schroeders
Bram Schroeders 2021년 1월 20일

0 개 추천

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
Radhika Kulkarni
Radhika Kulkarni 2021년 1월 21일
편집: Radhika Kulkarni 2021년 1월 21일
That works, thank you so much!!
Radhika Kulkarni
Radhika Kulkarni 2021년 1월 25일
편집: Radhika Kulkarni 2021년 1월 25일
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!

Translated by