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.
OCDER
OCDER 2017 年 10 月 12 日
You're welcome!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDICOM Format についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by