The answer seemed obvious at first, but it seems like another issue might be related to the indexing of X
getting broken if i
doesn't start at one. Here's one way to address that while handling pre-allocation of X
,
imgInds = 151:170;
di = dicominfo(sprintf('Brain_%04d.dcm',imgInds(1)));
X = zeros(di.Height,di.Width,1,numel(imgInds),class(dicomread(di))); % modify
for i = 1:numel(imgInds),
filename = sprintf('Brain_%04d.dcm', imgInds(i));
[X(:,:,1,i), amap] = dicomread(filename);
end
For clarity, I think it is better to build your sprintf
with %04d
instead of 01%02d
. You should set the size of X
accordingly on the line labeled modify
, particularly the third dimension since I assume your actual code will not have this be 1
.