Main Content


Replace voxel values for one slice of medical volume



medVolUpdated = replaceSlice(medVol,slice,direction,sliceValues) replaces the voxel values for one slice, slice, of the medicalVolume object medVol along the specified direction, direction. The replaceSlice function returns a new medicalVolume object with the updated values, specified by sliceValues.


collapse all

Replace one slice of 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 second slice in the transverse direction. The size of the extracted slice, X, is 512-by-512.

[X,position,spacings] = extractSlice(medVol,2,"transverse");
whos X
  Name        Size              Bytes  Class    Attributes

  X         512x512            524288  int16              

Specify a new 512-by-512 matrix to replace the extracted slice.

newX = ones(512);

Create a new medicalVolume object that replaces the extracted slice with the new slice.

medVolUpdated = replaceSlice(medVol,2,"transverse",newX);

Input Arguments

collapse all

Medical volume, specified as a medicalVolume object.

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 update the slice information, specified as "coronal", "sagittal", or "transverse".

Data Types: char | string

New voxel values, specified as a numeric array. The size of sliceValues must be the same size as the original slice in medVol specified by slice.

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

Output Arguments

collapse all

Medical volume with updated slice, returned as a medicalVolume object.

Version History

Introduced in R2022b