What is wrong with this code?

6 visualizzazioni (ultimi 30 giorni)
Yann Yiing
Yann Yiing il 11 Ott 2017
Commentato: OCDER il 12 Ott 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 Commenti
per isakson
per isakson il 11 Ott 2017
Replace
dcm = dcm2vol(C:\Users\ying0018\Documents\MATLAB)
by
dcm = dcm2vol('C:\Users\ying0018\Documents\MATLAB')
Jan
Jan il 11 Ott 2017
@per: If you post this solution as an answer, I could vote for it.

Accedi per commentare.

Risposta accettata

OCDER
OCDER il 11 Ott 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 Commenti
Yann Yiing
Yann Yiing il 12 Ott 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 il 12 Ott 2017
You're welcome!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su DICOM Format in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by