File Exchange

image thumbnail

Extract Slice from Volume

version (2.67 KB) by pangyuteng
ExtractSlice extracts an arbitrary slice, both intensities and indices, from a given volume.


Updated 12 Aug 2011

View Version History

View License

ExtractSlice extracts an arbitrary slice, both intensities and indices, from the input volume given the center and normal of the slice. If you are familiar with IDL, this is the equivalent function of EXTRACT_SLICE.

Cite As

pangyuteng (2021). Extract Slice from Volume (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (23)

maria teresa marino

Great function! thank you

javeria mughal

i need demo
how to use this code and how to extract radius value


Thanks for this excellent tool.
Is the output resolution of the slice the same as the resolution of the input volume?

@Andrey, is it possible to get a copy of your modification of the cubic interpolation of this function?

Andrey Gizdov

Slice planes almost never happen to have integer indices. This code rounds all indices before it extracts them from the volume, so all of the extracted data is inaccurate and in my case unusable, as very fine detail is required for my data.
I've edited the code to use cubic interpolation to get the color intensities at non- integer indices of the slice plane. Only downside is that it takes longer time...
Overall the code is very well structured.

Nathaniel H Werner

I'm trying to use this function but no matter what radius I pick I continue to NaNs for the slice output. Why might that be?

Eric Chadwick

This code very well put together. I will likely be using it in my master's thesis. I will make sure to give you credit where it is due.

Michael Turney

Thank you.

Paul C



Very useful.. Thank you very much..

Daniel Markel

I found an error when using this in 2013a occasionally when rotating the plane it would give me coordinates outside of the volume and thus return NaNs. I found changing line 95 to


fixes the problem by specifying the origin when rotating.



Jian Lin

Thank you very much! I really appreciate your work.
I downloaded the updated function and tested it. Yes, it gave me the unrounded xd (subX), yd (subY) and zd (subZ) which I needed. Great! Thanks!
Since I also need the values of the extracted plane from subX, subY and subZ (sorry for not metioning it in my previous email), I added the line to extractSlice before "for" loop,
h = slice(volume, subX, subY, subZ);
plane= get(h,'CData');
For a point on the plane, plane(i,j), I can get its coordinate [subX(i,j), subY(i,j) subZ(i,j)].
Hope what I did makes sense to you. I am happy to hear your comments.


Hi Jian, Please give the updated function a try!

Jian Lin

Do you have any idea how to extract the slice with unrounded xd, yd and zd in ExtractSlice?


Hi Jian, Feel free to change the code in any way you like, as long as it works for you!

Jian Lin

I checked again and was wrong with what I mentioned in my previous email. The shift is right!
After rotation, xd, yd and zd are rounded before "for" loop of ExtractSlice. I am wondering if it is possible for ExtractSlice to extract slice with xd,yd and zd without rounding.

Thanks for your help in advance.



Can you try to duplicate the same error with the example data as provided in the extractSlice function and paste the code here?

Jian Lin

Thank you, Ted.
I need to extract a plane perpendicular to the norm vector and pass a point (pX, pY, pZ) on the norm vector. I used command like
[theSlice, sliceInd] = extractSlice(volume,pX,pY,pZ,normX,normY,normZ,128)
The resultant plane is perpendicular to norm vector but not pass the point. (It seems to me the shift is not right.)
I am wondering how I can fix the problem if I can use the function to the purpose.




The coordinates (x,y,z) from 'ind2sub' will be the coordinates within the volume (original space).
'centerX', 'centerY' and 'centerZ' are the coordinates (in original space) of the center of the output slice.

Jian Lin

by the way, what is the exact meaning of the input parameters centerX,centerY,centerZ? Are they are the center of the original image volume or something else?



Jian Lin

thank you, Ted!
So you mean that sliceInd(:) or xyz after 'ind2sub' are still in the original space?




Hi Jian, If you just need the coordinates of the individual pixels in the slice, you can transform the coordinates of the pixels with the second output from extractSlice, ('sliceInd(:)'), by using function 'ind2sub'. Note that you may be getting NaNs if part of the slice is outside the volume. I hope this answers your question.

Jian Lin

Very cool and useful function!
I tried it and feel very easy to use it.
The function is just what I need. I really appreciate your work and thank you for your sharing the code.
But I need to transform the resultant plane back to the original coordinate space, too. Would it be any possible?


MATLAB Release Compatibility
Created with R2008b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!