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
採用された回答
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 件のコメント
Radhika Kulkarni
2021 年 1 月 20 日
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
2021 年 1 月 21 日
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
Radhika Kulkarni
2021 年 1 月 21 日
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]);
Radhika Kulkarni
2021 年 1 月 26 日
It returned this:

Bram Schroeders
2021 年 1 月 26 日
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
2021 年 1 月 26 日
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
2023 年 11 月 18 日
Thanks for your answers Bram Schroeders
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Contour Plots についてさらに検索
参考
2021 年 1 月 19 日
2023 年 11 月 18 日
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
