dicominfo returns an error on all dicom files other than one

22 views (last 30 days)
Hi all, i have a question. I'm writing some codes to read in the dicom tags of a series of dicom files. but somehow, dicominfo can only read the first data but returns an error on other data.
sourcedata = '/Users/.../dcmonly'
filesandfolders= dir([sourcedata '/**']);
%Plist = setdiff({ P_f([P_f.isdir]).name},{'.','..'});
all_list = {filesandfolders.name};
T = cell2table({});
for ii = 3: numel(all_list)
name = all_list{ii}
info(ii) = dicominfo(name);
s2det = [info.DistanceSourceToDetector];
s2pat = [info.DistanceSourceToPatient];
pixelspace = [info.ImagerPixelSpacing];
pixelsize = pixelspace * (s2det/s2pat);
Nf = 1/(2* pixelsize);
% write data to excel
T.s2det = s2det;
T.s2pat = s2pat;
T.pixelspace = pixelspace(1);
T.pixelsize = pixelsize(1);
T.Nf = Nf;
end
this is the error i got
Error using dicom_getFileDetails (line 14)
Unable to load file "00000018.dcm".
Error in dicominfo (line 55)
fileDetails = dicom_getFileDetails(filename);
Error in dicomtags (line 17)
info(ii) = dicominfo(name);
FYI the code after %write data to excel is pretty self explanatory, but i wanna append every info from other dicom files too in the sam excel file. i'm not sure how to go about this either. any help would be very much appreciated!

Accepted Answer

Simon Chan
Simon Chan on 25 Jul 2021
Below shows some modification based on your code:
(1) Variable all_list is going to contain file names only, so that the index can start from 1 in the for loop
(2) Not indexing the varaible info so that it overwrites every time in the loop. You only needs to put the indexing unless you would like to record all information and process them later.
(3) Verify variable info.DistanceSourceToPatient is not zero in your case. Some vendor does not record this value and just put zero. If this is zero, you cannot calculate the pixel size later in the code
(4) info.PixelSpacing normally has two values, one for x and one for y-dimension. They are the same in most of the case but better to verify it by typing info.PixelSpacing to see both values. The following code take one dimension to calculate the pixelspace only (should be enough).
(5) Result for each dicom image is saved inside the loop and append in the array exceldata
(6) Write data to excel after finishing the loop
sourcedata = '/Users/.../dcmonly'; % Your working directory
filesandfolders= dir([sourcedata '/**']); % Read all files and folders
all_list = {filesandfolders(~[filesandfolders.isdir]).name}; % Retrieves files only
exceldata = []; % Initialize an empty array
for ii = 1: numel(all_list)
name = all_list{ii}; % Read the file name
info = dicominfo(name); % Retrieve the dicom header
s2det = info.DistanceSourceToDetector;
s2pat = info.DistanceSourceToPatient;
pixelspace = info.PixelSpacing(1);
pixelsize = pixelspace * (s2det./s2pat);
Nf = 1/(2* pixelsize);
exceldata = [exceldata; s2det s2pat pixelspace pixelsize Nf]; % Append the data
end
writematrix(exceldata,fullfile(sourcedata,'result.xlsx'),'UseExcel',true,'Sheet','Extra','WriteMode','append');
  8 Comments
Simon Chan
Simon Chan on 25 Jul 2021
Now I understand, I was thinking all the data writing to excel are numeric and hence I use writematrix. If there is string in the data, I would use writecell as follows:
The code below also write the file name in the first column as an example.
It includes the headers in the first row of the file as well. You may change the name if you like.
sourcedata = '/Users/.../dcmonly'; % Your working directory
filesandfolders= dir([sourcedata '/**']); % Read all files and folders
all_list = {filesandfolders(~[filesandfolders.isdir]).name};
exceldata = {'File name', 's2det', 's2pat', 'pixelspace', 'pixelsize', 'Nf'}; % Initialize an empty cell array
for ii = 1: numel(all_list)
name = all_list{ii}; % Read the file name
info = dicominfo(name); % Retrieve the dicom header
s2det = info.DistanceSourceToDetector;
s2pat = info.DistanceSourceToPatient;
pixelspace = info.PixelSpacing(1);
pixelsize = pixelspace * (s2det./s2pat);
Nf = 1/(2* pixelsize);
data = horzcat({name}, {s2det}, {s2pat}, {pixelspace}, {pixelsize}, {Nf});
exceldata = vertcat(exceldata, data);
end
writecell(exceldata,fullfile(sourcedata,'result.xlsx'),'UseExcel',true,'Sheet','Extra','WriteMode','append');
Somthing like this should be found in the excel file, noticed that the data shown below are only for demo purpose, not real data.

Sign in to comment.

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!