MATLAB: Using Slice on a Cartesian Grid that was Generated in Cylindrical Coordinates

3dcontourcontoursliceslice

Hello,
I generate a grid in cylindrical coordinates:
phi_grid = 0:1:360 ;
rho_grid = 5.5:0.1:10;
z_grid = -4:0.1:4;
[Rho_grid,Z_grid,Phi_grid] = meshgrid(rho_grid,z_grid,phi_grid);%creating a grid from those axes
I then convert to spherical & cartesian coordinates for when I need them.
R_grid = (Rho_grid.^2 + Z_grid.^2).^(0.5);% computing a Rad value for each point
Theta_sys3_grid = acosd(Z_grid./R_grid);
x = R_grid.*sind(Theta_sys3_grid).*cosd(Phi_grid);
y = R_grid.*sind(Theta_sys3_grid).*sind(Phi_grid);
z = R_grid.*cosd(Theta_sys3_grid);
I then run code to produce a value at each of these points in the cartesian plane. This code operates on the spherical coordinates. That code is long and complicated, here is replacement
v = x.*exp(-x.^2-y.^2-z.^2);
I now want to make a slice of the this data in the cartesian plane
xslice = linspace(-6,10,20);
yslice = linspace(-6,10,40);
zslice = linspace(-4,4,40);
xslice = xslice';
zslice = zslice';
yslice = yslice';
figure()
slice(x,y,z,v,xslice,yslice,zslice)
And I am returned this error message: "Input grid is not a valid MESHGRID." This is not an issue that one normally has if their grid was created using meshgrid. However, I'm assuming that creating a grid in one coordinate system and then changing the coordinate system can make the data either not monotonic or not plaid, compromising its valuable MESHGRID attributes. What I think I'm looking for, is a way make my new grids monotonic and plaid so I can operate on them with slice and contour slice, or, alternative solutions
Thank you
pH

Best Answer

One thing to try would be to use your x,y,z, and v to interpolate new data that is a meshgrid. It might run a little slower than you'd like but it would look something like:
%Create the new meshgrid to slice
[newX, newY, newZ] = meshgrid(-10:0.5:10, -10:0.5:10, -4:0.5:4);
%Interpolate
newV = griddata(x,y,z,v,newX,newY,newZ);
figure();
slice(newX, newY, newZ, newV, xslice, yslice, zslice);
Here is the related example in the documentation: https://www.mathworks.com/help/matlab/ref/griddata.html#bvkwypt-2