MATLAB Answers

Problem with sprintf and read DICOM images

7 views (last 30 days)
Veronika on 4 Apr 2017
Commented: Walter Roberson on 17 Nov 2017
Dear all,
I have CT slices and I would like to load (imread) them to the Workspace. But I always read only one (first) slice (image). This is my code:
for p=1:38
filename = sprintf('C:/Users/ced0005/DP/DP_framework/DICOM/slicesCT/000048.dcm',p);
X(:,:,1,p) = dicomread(filename);
I don´t know, where mistake is.
Can you advise me? Thank you for your answers.


Sign in to comment.

Accepted Answer

Jan on 4 Apr 2017
Edited: Jan on 4 Apr 2017
There is no format specifier in the format string of sprintf:
This is the same string for all iterations. Perhaps you want:
filename = sprintf('C:/Users/ced0005/DP/DP_framework/DICOM/slicesCT/%06d.dcm', p);


Show 9 older comments
Walter Roberson
Walter Roberson on 17 Nov 2017
last_to_read = 135;
for file_number = 1 : last_to_read
filename = fullfile(Folder, sprintf('CTAC001_CT%03d.dcm', file_number));
X(:, : , 1, file_number) = dicomread(filename);
if file_number == 1 && last_to_read ~= 1
X(end, end, end, last_to_read) = 0;
The special test for file_number 1 is a performance optimization to avoid having to grow the array every iteration.
mohd akmal masud
mohd akmal masud on 17 Nov 2017
Dear Walter Roberson,
The code is successfully. I have try for 135 slices CT images and also 135 slices PET images.
But how come to me to show it in figure (i mean use command imshow)
Walter Roberson
Walter Roberson on 17 Nov 2017
It cannot be shown in a single figure using imshow. imshow() is not suitable for volume visualization.
If you have R2017a or newer, try
volumeViewer( permute(X, [1 2 4 3]) )

Sign in to comment.

More Answers (0)

Translated by