sorting files into 2 separate folders

3 views (last 30 days)
Hoda
Hoda on 13 Feb 2015
Edited: Hoda on 14 Feb 2015
Hi I am trying to sort some DICOM files (1584), by separating them and saving them into 2 separate folders. the way i need to separate them is every alternating 8 files goes to one of the 2 folders. so i think i need start counting 1-8 and put in 1 folder and the next 8 go to the other folder. I'm not sure how to proceed this is what i have till now
% sorting alternating data
path = 'C:\Users\......';
output_dir = uigetdir('','C:\Users\Sorted_GE_SE_Images');
dirs = dir('C:\Users\GE_SE_Images\....\10_ep2d_ge_se_128_DCE\*.dcm')
N = length(dirs);
for i = 1 : N;
for j = 1:8;
file_SE = dirs(j).name
copyfile(file_SE,'C:\Users\\Sorted_GE_SE_Images\SE_images')
for k = 9:16;
file_GE = dirs(k).name
copyfile(file_GE,'C:\Users\Sorted_GE_SE_Images\SE_images')
end
end
end

Accepted Answer

per isakson
per isakson on 14 Feb 2015
Maybe this will help you
sorting alternating data
path = 'C:\Users\......';
output_dir = uigetdir('','C:\Users\Sorted_GE_SE_Images');
dirs = dir('C:\Users\GE_SE_Images\....\10_ep2d_ge_se_128_DCE\*.dcm');
N = length(dirs);
for ii = 1 : 16 : N
sad = dirs(ii:min([N,ii+16]));
for jj = 1:length(sad)
file_SE = dirs(jj).name;
if jj<=8
copyfile( file_SE, 'C:\Users\Sorted_GE_SE_Images\SE_images')
else
copyfile( file_SE, 'C:\Users\Sorted_GE_SE_Images\GE_images')
end
end
end
  2 Comments
Hoda
Hoda on 14 Feb 2015
Edited: per isakson on 14 Feb 2015
Thanks alot Per, if you dont mind could you explain what this command
sad = dirs(ii:min([N,ii+16]));
does. Also tried to run your code but I get an error : Error using copyfile No matching files were found.
per isakson
per isakson on 14 Feb 2015
The outer loop takes steps of 16 items of the file list except for the last where it takes the rest.
for ii = 1 : 16 : N
sad = dirs(ii:min([N,ii+16]));
sixteen items of the file list are copied to sad. The inner loop loops over the length of sad. Copying to sad makes the indexing simpler.
&nbsp
"Error using copyfile No matching files were found" &nbsp Replace
file_SE
by
fullfile( 'folder_name', file_SE )

Sign in to comment.

More Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!