MATLAB: How to select files in a directory

dir

Hello,
I created a directory which contains a lot of DICOM files (IM_0001, IM_0004, …, IM_0025, IM_0028, IM_0031, …, IM_0052, …, IM_0088) using :
listing = dir('IM*.*');
I would like to select only files which names are between IM_0025 and IM_0052 (IM_0025, IM_0028, IM_0031, …, IM_0052) and save them in another directory.
Is there any function that may help me ?
Thank you in advance !

Best Answer

listing = dir('IM*.*');
for K = 1 : length(listing)
fname = listing(K).name;
if ~strcmp(fname(1:3), 'IM_'); continue; end
fnum = str2double(fname(4:7));
if isnan(fnum) || fnum < 25 || fnum > 52; continue; end
copyfile(fname, 'NewDirectoryNameGoesHere');
end
or use movefile() instead of copyfile() if you want them moved instead of duplicated.