-
Notifications
You must be signed in to change notification settings - Fork 6
/
organizeDicoms.m
38 lines (32 loc) · 1.29 KB
/
organizeDicoms.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
function organizeDicoms(dicomFolder, outFolder)
% FORMAT organizeDicoms(dicomFolder, outFolder)
% Copy and rename dicom files based on ProtocolName and SeriesNumber.
%
%
% Inputs:
% dicomFolder: Single folder where all of the dicoms are for a single
% appointment.
% outFolder: Folder where dicoms will be organized (in generated
% subfolders).
dicomFiles = dir([dicomFolder '/*.dcm']);
disp('Organizing dicoms.');
for iFile = 1:length(dicomFiles)
dicomHeader = dicominfo([dicomFolder '/' dicomFiles(iFile).name]);
seriesNumber = num2str(dicomHeader.SeriesNumber);
protocolName = strrep(dicomHeader.ProtocolName, ' ', '_');
outSubFolder = [seriesNumber '_' protocolName];
if ~exist([outFolder '/' outSubFolder '/'], 'dir')
outFolders{counter} = [outFolder '/' outSubFolder '/'];
mkdir(outFolders{counter});
counter = counter + 1;
end
system(['cp ' dicomFolder '/' dicomFiles(iFile).name ' ' outFolder '/' outSubFolder]);
end
disp('Renaming dicoms.');
for iFolder = 1:length(outFolders)
seriesDicoms = dir([outFolders{iFolder} '/*.dcm']);
for jFile = 1:length(seriesDicoms)
system(['mv ' outFolders{iFolder} '/' seriesDicoms(jFile).name ' ' outFolders{iFolder} '/' sprintf('%04d', jFile)]);
end
end
end