how to resolve index exceeds matrix dimensions in matlab?

1 view (last 30 days)
i am trying to read series of dicom images from a folder named as series 8.below is code to read series of dicom images from a particular folder.i am getting
error index exceeds matrix dimensions at
info = dicominfo(fullfile(fileFolder,fileNames{1})).
----
clear all;
close all;
clc;
fileFolder = fullfile(pwd, 'series 8');
files = dir ( fullfile (fileFolder, '*.dcm'));
fileNames = {files.name};
%examine file header (metadata , from dicom stack)
info = dicominfo(fullfile(fileFolder,fileNames{1}))
%extract size info from metadata
voxelsize = [info.PixelSpacing;info.SliceThickness];
%read one file to get size
I = dicomread(fullfile(fileFolder,fileNames{1}))
classI = class(I);
sizeI = size(I);
numImages = length(fileNames);
%read slice images populate 3d matrix
hWaitBar = waitbar(0,'reading dicom files');
%create array
mri= zeroes(info.rows , info.columns , numImages , classI )
for i=length(fileNames):-1:1
fname = fullfile(fileFolder, fileNames{i});
mri(:,:,i) = unit16(dicomread(fname));
waitbar((length(fileNames)-i+1)/length(fileNames))
end
delete(hWaitBar);

Answers (2)

Walter Roberson
Walter Roberson on 13 Nov 2015
You would get that error if there is no 'series 8/*.dcm' relative to the starting directory. Your code assumes that at least one file name was returned, without testing for that being true.

Image Analyst
Image Analyst on 13 Nov 2015
Edited: Image Analyst on 13 Nov 2015
Normal debugging procedures would suggest you do it in two or more steps:
fileFolder = fullfile(pwd, 'series 8')
filePattern = fullfile (fileFolder, '*.dcm')
files = dir(filePattern);
fileNames = {files.name};
if isempty(fileNames)
message = sprintf('There are no *.dcm files in the folder:\n%s', fileFolder);
uiwait(warndlg(message));
return;
end
% If we get here, there is at least one file.
% Examine file header (metadata , from dicom stack) from the very first file.
fullFileName = fullfile(fileFolder, fileNames{1})
if exist(fullFileName, 'file')
successMessage = sprintf('File does indeed exist:\n%s', fullFileName);
uiwait(helpdlg(successMessage ));
info = dicominfo(fullFileName)
else
warningMessage = sprintf('File does NOT exist:\n%s', fullFileName);
uiwait(warndlg(warningMessage));
end
Now what do you see?
EDIT I edited it to add a popup warning message if there are no dcm files present in the folder.
More related code samples are in the FAQ http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F, like how to process all the files instead of just the first one.

Community Treasure Hunt

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

Start Hunting!