How to plot data on a cylindrical grid using slice?

此生再无相见时 提交于 2021-01-28 20:46:24

问题


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?


回答1:


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.




回答2:


Can you just call meshgrid on the Cartesian coordinates?

r   = linspace(1,4,4);
phi = linspace(0,2*pi,10);
z   = linspace(1,3,3);

[x, y] = pol2cart(r,phi);

[xmesh,ymesh,zmesh]=meshgrid(x, y, z);

btw sometimes you might find [ymesh,xmesh,zmesh]=meshgrid(x, y, z); makes more sense for your application.



来源:https://stackoverflow.com/questions/14316603/how-to-plot-data-on-a-cylindrical-grid-using-slice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!