how to resolve index exceeds matrix dimensions in matlab?

조회 수: 5 (최근 30일)
santosh patil
santosh patil 2015년 11월 13일
편집: Image Analyst 2015년 11월 13일
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);

답변 (2개)

Walter Roberson
Walter Roberson 2015년 11월 13일
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.
  댓글 수: 2
santosh patil
santosh patil 2015년 11월 13일
@Walter Roberson but there are dicom files in series 8 folder..
Stephen23
Stephen23 2015년 11월 13일
What does size(fileNames) show?

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2015년 11월 13일
편집: Image Analyst 2015년 11월 13일
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.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by