Interpolating contour plot using user input
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
2021년 1월 20일
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.

Bram Schroeders
2021년 1월 21일
편집: Bram Schroeders
2021년 1월 21일
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) "
Bram Schroeders
2021년 1월 21일
편집: Bram Schroeders
2021년 1월 21일
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
2021년 1월 21일
편집: Radhika Kulkarni
2021년 1월 21일
That works, thank you so much!!
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!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
