Hey All, I was wondering if there was a way to modify the griddata program to include plotting. Given that I am using data points I know I will have to use meshgrid and was trying to figure out how it would be possible to include into the program. Any ideas? Mel

댓글 수: 3

Walter Roberson
Walter Roberson 2011년 5월 25일
Why not just call meshgrid() after the griddata() call, as shown in the example in the documentation ?
Matt Fig
Matt Fig 2011년 5월 25일
I think you mean MESH, not MESHGRID.
Melissa
Melissa 2011년 5월 25일
As I am trying to implement in a GUI the command window will not be available. When I plug this code into each if statement (ie linear, cubic, nearest) I only receive a plot of the points and not of the generated surfaces. Any suggestions?
%Plotting Griddata
plot3(x,y,z,'o')
hold on
[X,Y]=meshgrid(xi,yi);
surf(X,Y,zi)
hold off

댓글을 달려면 로그인하십시오.

 채택된 답변

Matt Fig
Matt Fig 2011년 5월 25일

0 개 추천

From the doc:
x = rand(100,1)*4-2;
y = rand(100,1)*4-2;
z = x.*exp(-x.^2-y.^2);
ti = -2:.25:2;
[XI,YI] = meshgrid(ti,ti);
ZI = griddata(x,y,z,XI,YI);
mesh(XI,YI,ZI), hold
plot3(x,y,z,'o'), hold off
%
%
%
%
EDIT
So do this:
>> edit griddata % Once the file is up, do this:
ctrl+a,ctrl+c,ctrl+n,ctrl+v,ctrl+s % I recommend using mygriddata
Then look in the new file, and find the last switch statement of the main function. Immediately after that switch statement, put this:
mesh(xi,yi,zi)
hold on
plot3(x,y,z,'o')
hold off
Or whatever you want.

댓글 수: 11

Melissa
Melissa 2011년 5월 25일
Thank you for responding. I understand how to plot in the command window using the examples but I wanted to put inside teh griddata code to plot automatically.
Walter Roberson
Walter Roberson 2011년 5월 25일
Melissa, in my experience that is almost certainly the wrong approach to what you are doing.
Matt Fig
Matt Fig 2011년 5월 25일
See my edit above...
Melissa
Melissa 2011년 5월 26일
Again thank you so much for taking the time to help me I really appreciate it. I placed it after the switch function given the cases and it produced the error that Z must be a matrix and not a vector or scalar. I don't understand why this works in the command window and not the program itself.
Matt Fig
Matt Fig 2011년 5월 26일
How did you call the new function? Because I called it like this:
x = rand(100,1)*4-2;
y = rand(100,1)*4-2;
z = x.*exp(-x.^2-y.^2);
ti = -2:.25:2;
[XI,YI] = meshgrid(ti,ti);
ZI = mygriddata(x,y,z,XI,YI); % Call new function, made as above
And it worked perfectly. Note that I called the new function.
Melissa
Melissa 2011년 5월 26일
I think I am leaving out the meshgrid...hm I wonder if I can place that above the plotting code.
x =[-2 0 1 5 10 6]
y =[-3 7 4 0 3 -1]
z =[-12 7 8 8 4 0]
xi =0:1:10
yi =0:1:10
mygriddata(x,y,z,xi,yi)
Melissa
Melissa 2011년 5월 26일
it works perfectly if I use meshgrid in the command window but why cant I use meshgrid within the program itself? I placed it right above the code you have given. Which I very much appreciate and am so thankful for you showing me where to place it.
Matt Fig
Matt Fig 2011년 5월 26일
If you want to be able to call MYGRIDDATA as you did above, put this as the first line of the MYGRIDDATA:
[xi,yi] = meshgrid(xi,yi);
But notice that this will mess up your use of the function the other way... It will indeed be a custom function.
Melissa
Melissa 2011년 5월 26일
life saver. thank you.
Walter Roberson
Walter Roberson 2011년 5월 26일
I still say it was the wrong approach. If you were going to create a new function, why not create a function that contained the lines I showed and which called the unmodified griddata() ?
Matt Fig
Matt Fig 2011년 5월 26일
That is how I would do it, Walter. But making a custom function from a built-in is o.k. too, I think. I have done so in the past. The thing to avoid is altering the MATLAB version.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2011년 5월 25일

0 개 추천

[X,Y] = meshgrid(xi,yi);
Z = griddata(x,y,z,X,Y);
mesh(X,Y,Z), hold
plot3(x,y,z,'o'), hold off

댓글 수: 2

Melissa
Melissa 2011년 5월 25일
that only works in the command window.
Walter Roberson
Walter Roberson 2011년 5월 25일
Perhaps we are misunderstanding each other, but there is no reason why the code I show above would not work in a script or function, including a callback function.
At most it might be necessary to be more careful about which axes to plot on to.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

질문:

2011년 5월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by