What is wrong with this code?

6 次查看(过去 30 天)
Yann Yiing
Yann Yiing 2017-10-11
评论: OCDER 2017-10-12
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 个评论
per isakson
per isakson 2017-10-11
Replace
dcm = dcm2vol(C:\Users\ying0018\Documents\MATLAB)
by
dcm = dcm2vol('C:\Users\ying0018\Documents\MATLAB')
Jan
Jan 2017-10-11
@per: If you post this solution as an answer, I could vote for it.

请先登录,再进行评论。

采纳的回答

OCDER
OCDER 2017-10-11
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 个评论
Yann Yiing
Yann Yiing 2017-10-12
Thanks @Donald Lee for the improvement now its looks better. Thanks @per isakson's for the comment above to fix the error.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 DICOM Format 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by