Rename DICOM folder using DICOM info


Set input and output dir, then run. The program will search each folder in input dir, read DICOM info from first image of each stack and set age, name and gender as output folder name.

maindir = 'E:\C-Tsinghua\dataF\201607211307';
Destdir='E:\C-Tsinghua\dataF';
subdir  = dir( maindir );
for i = 1 : length( subdir )
    if( isequal( subdir( i ).name, '.' )||...
        isequal( subdir( i ).name, '..')||...
        ~subdir( i ).isdir)               
        continue;
    end
    subdirpath= fullfile( maindir, subdir( i ).name);
    subdir2=dir( subdirpath );
    
    subdir2path = fullfile( maindir, subdir( i ).name,subdir2(3).name,'*.dcm' );
    dat = dir( subdir2path ) ;             
    filename = fullfile( maindir, subdir( i ).name, subdir2(3).name,dat(3).name);
    metadata=dicominfo(filename);
    patientname=metadata.PatientName.FamilyName;
    sepind=find(patientname(:)==' ');
    sepend=size(patientname,2);
    if size(sepind,2)>1
        sepend=sepind(2);
        sepind=sepind(1);
    end
    patientnamen=[patientname(1) lower(patientname(2:sepind-1)) patientname(sepind+1) lower(patientname(sepind+2:sepend))];
    patientsex=metadata.PatientSex;
    patientage=str2num(metadata.ContentDate(1:4))-str2num(metadata.PatientBirthDate(1:4));
    newfoldername=[mat2str(patientage), '_', patientnamen, '_' , patientsex];
    movfrom=fullfile( maindir, subdir( i ).name,subdir2(3).name);
    movto=fullfile(Destdir,newfoldername);
    movefile(movfrom,movto);
end



Last Article Next article

Comment 评论



Share 分享

New Users 最新加入

  • hokurikustr

  • refrain

New comments 最新评论

test123: aasdas Details Apr 13 16:39
admin: Thanks! Details Apr 09 11:46
admin: Google map api Details Apr 09 11:46
lqj12: cooooooooool Details Apr 08 21:34
Yunhan Huang: 这个功能是如何实现的? Details Apr 08 13:23