How to extract values within a circular region from a griddata?

19 views (last 30 days)
I have a 2d matrix (100 x 100 size) obtained from the piece of code is as follows:
N = 100;
xv = linspace(min(CoordsX), max(CoordsX), N);
yv = linspace(min(CoordsY), max(CoordsY), N);
[X,Y] = ndgrid(xv, yv);
Data = griddata(CoordsX, CoordsY, Field, X, Y);
figure()
surf(X, Y, Data) % This make a surface plot of Data.
'Data' outputs a 100 x 100 matrix and then I plot a surface plot as it is in the code. Here I want to select a region with in a circle centered at xc, yc and radius 'r'. Then make another surface plot. How can I extract the vlaues from 'Data' those are within a circular region or radius 'r'?
Thank you

Accepted Answer

Bjorn Gustavsson
Bjorn Gustavsson on 23 Jun 2022
You "simply" only ask for the values inside that circular region. You can do it at least two different ways:
N = 100;
xv = linspace(min(CoordsX), max(CoordsX), N);
yv = linspace(min(CoordsY), max(CoordsY), N);
[X,Y] = ndgrid(xv, yv);
Data = griddata(CoordsX, CoordsY, Field, X, Y);
figure()
subplot(2,2,1)
surf(X, Y, Data) % This make a surface plot of Data.
Ioutside = find((X(:)-xc).^2+(Y(:)-yc).^2>r);
Data(Ioutside) = nan;
surf(X, Y, Data) % This make a surface plot of Data inside the circular region.
phi360 = (0:360)*pi/180;
r = linspace(0,r);
[Phi360,R] = meshgrid(phi360,r);
Data = griddata(CoordsX, CoordsY, Field, R.*cos(Phi360), R.*sin(Phi360));
subplot(2,2,3)
surf(R.*cos(Phi360), R.*sin(Phi360), Data) % This make the surface-plot with cylindrical coords
You could(should) also take a look at the scatteredInterpolant function since that might be a useful and more flexible multi-call replacement of the griddata-function.
HTH

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!