What is wrong with this code?
6 次查看(过去 30 天)
显示 更早的评论
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
2017-10-11
Replace
dcm = dcm2vol(C:\Users\ying0018\Documents\MATLAB)
by
dcm = dcm2vol('C:\Users\ying0018\Documents\MATLAB')
采纳的回答
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)
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!