Main Content


Extract voxels and spatial details for one slice of medical volume

Since R2022b



[X,position,spacings] = extractSlice(medVol,slice,direction) extracts the voxel data and spatial information for one slice, slice, of the medicalVolume object medVol along the specified direction, direction.


collapse all

Extract a slice from a medical image volume created using a chest CT volume saved as a directory of DICOM files. The CT volume is part of a data set containing three CT volumes. The size of the entire data set is approximately 81 MB. Download the data set from the MathWorks® website, then unzip the folder.

zipFile = matlab.internal.examples.downloadSupportFile("medical","");
filepath = fileparts(zipFile);

Specify the directory of DICOM files for the first CT volume in the data set.

dataFolder = fullfile(filepath,"MedicalVolumeDICOMData","LungCT01"); 

Create a medical volume object for the CT volume.

medVol = medicalVolume(dataFolder);

Extract the third slice in the transverse direction.

[X,position,spacings] = extractSlice(medVol,3,"transverse");

The X output contains the voxel data for the extracted slice.

whos X
  Name        Size              Bytes  Class    Attributes

  X         512x512            524288  int16              

The position output provides the patient coordinates of the first voxel in the slice, in millimeters.

position = 1×3

 -186.5000 -186.5000 -276.2500

The spacings output provides the in-plane pixel spacing within the extracted slice, in millimeters.

spacings = 1×2

    0.7285    0.7285

Input Arguments

collapse all

Medical volume, specified as a medicalVolume object.


The extractSlice function is not valid if the Orientation property value of medVol is "mixed", "oblique", or "unknown". In these cases, access the voxel data stored in the Voxels property directly using array indexing.

Slice index, specified as a positive integer scalar in the range [1, numSlices], where numSlices is the number of slices in the volume along the direction specified by direction.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical

Direction along which to extract the slice information, specified as "coronal", "sagittal", or "transverse".

Data Types: char | string

Output Arguments

collapse all

Voxel data of the extracted slice, returned as an m-by-n numeric matrix, where m and n are the number of rows and columns in the image slice in the plane specified by direction. The X output array is the same data type as the Voxels property of medVol.

Position of the upper-left pixel of the extracted slice, returned as a 3-element numeric vector of the form [x y z]. The coordinates are in the patient coordinate system, in the units specified by the SpatialUnits property of medVol.

Spacing between voxel centers in the first two dimensions of the extracted slice, returned as a 2-element numeric vector of the form [dim1spacing dim2spacing]. The values are in the units specified by the SpatialUnits property of medVol.

Version History

Introduced in R2022b