I have input data on a cylindrical grid and want to plot them using slice
in MATLAB.
To do this I first transform the reference coordinates into Cartesian coordinates using pol2cart
.
r = linspace(1,4,4);
phi = linspace(0,2*pi,10);
z = linspace(1,3,3);
[rmesh,phimesh,zmesh]=meshgrid(r,phi,z)
[xmesh,ymesh,zmesh]=pol2cart(phimesh,rmesh,zmesh)
When I use slice
now (e.g. slice(xmesh,ymesh,zmesh,ones(10,4,3),2,2,2)
) an error is thrown, because the coordinate matrices are not ordered correctly (Input grid is not a valid MESHGRID.
)
How can I modify the matrices to get a plottable result?
Sadly you cannot use data given in cylindrical coordinates for usage with slice.
From the matlab documentation:
slice(X,Y,Z,V,sx,sy,sz) draws slices of the volume V. X, Y, and Z are three-dimensional arrays specifying the coordinates for V.
X, Y, and Z must be monotonic and orthogonally spaced (as if produced by the function meshgrid).
What you can do is use griddata
.
here is an example:
r = linspace(1,4,4);
phi = linspace(0,2*pi,10);
z = linspace(1,3,3);
data = repmat(linspace(1,0,4),[10,1,3]);
[rmesh,phimesh,zmesh]=meshgrid(r,phi,z);
[xmesh,ymesh,zmesh]=pol2cart(phimesh,rmesh,zmesh);
[xg, yg, zg] = meshgrid(linspace(-4,4,50),linspace(-4,4,50),linspace(1,3,3));
gdata = griddata(xmesh,ymesh,zmesh,data,xg,yg,zg);
slice(xg,yg,zg,gdata,2,2,2)
Depending on what kind of data you have and how important it is not to display data that is 'out of bounds' (meaning, following your example: of radius smaller than 1 or larger than 4) you can add the following to hide data that is out of your area of interest:
rg = sqrt(xg.^2+yg.^2);
gdataNaN = gdata;
gdataNaN(rg<min(r)) = NaN;
gdataNaN(rg>max(r)) = NaN;
figure
slice(xg,yg,zg,gdataNaN,2,2,2)
If that isn't enough you would have to implement your own slice
method (basically using the griddata method) or look on matlab central fileexchange.
I haven't tested it, but tools for analyzing MRI images might do the trick (check, for example, this: http://www.mathworks.com/matlabcentral/fileexchange/27983-3d-slicer ).
EDIT: http://www.mathworks.com/matlabcentral/fileexchange/30147-smartslice-and-igslice this seems to have been developed by someone with the same problem.