MATLAB: If a variable holds a bunch of images, what does (:, :, 1, i) mean

Image Processing Toolboximages

Part of the code –
img = dicomread('example.dcm');
for i=1:100
data(:, :, i) = img(:, :, 1, i);
end

Best Answer

The code you have assumes that the file contains a multiframe image with at least 100 frames. If the file has less frames the code will error and if it has more, it will miss these extra frames. It would be much better to use the actual number of frames instead of hardcoding a constant:
for i = 1:size(img, 4) %go over all the frames, however many there are
img(:, :, :, i) is a 3D matrix, representing the RGB values of frame i, where img(:, :, 1,i) is the red channel, img(:, :, 2, i) is the green channel and img(:, :, 3, i) the blue channel. In effect your code copies all the red channels into a new 3D matrix and discard the green and blue channels. Possibly the code expects the images to be grayscale so all 3 channels should be equal.
The loop is completely unnecessary, the same can be achieve simply with:
img = dicomread('example.dcm')
data = squeeze(img(:, :, 1, :)); %img(:, :, 1, :) will result in a H x W x 1 x NumFrames matrix. squeeze converts that into a H x W x NumFrames matrix
Related Question