What is wrong with this code?

1 view (last 30 days)
Yann Yiing
Yann Yiing on 11 Oct 2017
Commented: OCDER on 12 Oct 2017
Code:
Function dcm = dcm2vol(din)
%%import dicom volume
ls = dir(fullfile(din,'*.dcm'));
s = size(ls);
tmp = dicomread([din,'\',ls(1).name]);
si = size(tmp);
Yr = NaN(si(1),si(2),s(1));
Yr(:,:,1) = tmp;
for i = 2:s(1)
Yr(:,:,i) = dicomread([din,'\',ls(i).name]);
end
dcm = Yr;
end
din refers to directory. However when i use this as the directory i get error.
>> dcm = dcm2vol(C:\Users\ying0018\Documents\MATLAB)
dcm = dcm2vol(C:\Users\ying0018\Documents\MATLAB)
Error: Unexpected MATLAB operator.
  2 Comments
per isakson
per isakson on 11 Oct 2017
Replace
dcm = dcm2vol(C:\Users\ying0018\Documents\MATLAB)
by
dcm = dcm2vol('C:\Users\ying0018\Documents\MATLAB')
Jan
Jan on 11 Oct 2017
@per: If you post this solution as an answer, I could vote for it.

Sign in to comment.

Accepted Answer

OCDER
OCDER on 11 Oct 2017
Here are some other comments about the code to help with readability, etc:
Function dcm = dcm2vol(din) %<-- "function" must be lowercase
%%import dicom volume
ls = dir(fullfile(din,'*.dcm')); %Don't use "ls" as a variable since "ls" a function
s = size(LS);
tmp = dicomread([din,'\',LS(1).name]); %Use "fullfile" like you did above, or "filesep" instead of '\' to be platform independent.
%I see you want to preallocate but don't know the size yet. Your method
%works, but for readability, see the other method too.
si = size(tmp);
Yr = NaN(si(1),si(2),s(1)); %Will there be any NaN values after dicomread?
Yr(:,:,1) = tmp;
for i = 2:s(1)
Yr(:,:,i) = dicomread([din,'\',LS(i).name]);
end
dcm = Yr; %<-- No need to use temp variable Yr when it's same as dcm
Here's a rewritten version of your code:
function dcm = dcm2vol(din)
%%import dicom volume
LS = dir(fullfile(din,'*.dcm'));
s = size(LS);
Yr = cell(s(1), 1);
for i = 1:s(1)
Yr{i} = dicomread(fullfile(din,LS(i).name));
end
dcm = cat(3, Yr{:});
To run dcm2vol, see @per isakson's comment above (which is the answer to your original error)
  2 Comments
Yann Yiing
Yann Yiing on 12 Oct 2017
Thanks @Donald Lee for the improvement now its looks better. Thanks @per isakson's for the comment above to fix the error.
OCDER
OCDER on 12 Oct 2017
You're welcome!

Sign in to comment.

More Answers (0)

Categories

Find more on DICOM Format in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!