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

 Respuesta aceptada

Bram Schroeders
Bram Schroeders el 20 de En. de 2021

0 votos

I think taking a look at this documentation may help you https://www.mathworks.com/help/matlab/ref/scatteredinterpolant.html

13 comentarios

Radhika Kulkarni
Radhika Kulkarni el 20 de En. de 2021
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.
Radhika Kulkarni
Radhika Kulkarni el 21 de En. de 2021
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.
Bram Schroeders
Bram Schroeders el 21 de En. de 2021
Editada: Bram Schroeders el 21 de En. de 2021
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
Radhika Kulkarni
Radhika Kulkarni el 21 de En. de 2021
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) "
Bram Schroeders
Bram Schroeders el 21 de En. de 2021
Editada: Bram Schroeders el 21 de En. de 2021
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 el 21 de En. de 2021
Editada: Radhika Kulkarni el 21 de En. de 2021
That works, thank you so much!!
Radhika Kulkarni
Radhika Kulkarni el 25 de En. de 2021
Editada: Radhika Kulkarni el 25 de En. de 2021
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]);
Radhika Kulkarni
Radhika Kulkarni el 26 de En. de 2021
It returned this:
Bram Schroeders
Bram Schroeders el 26 de En. de 2021
This is exactly what i would expect that it returns. If this is not what you want, i don't understand your question.
Radhika Kulkarni
Radhika Kulkarni el 26 de En. de 2021
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
piston_pim_offset
piston_pim_offset el 18 de Nov. de 2023
Thanks for your answers Bram Schroeders

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Contour Plots en Centro de ayuda y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by