Borrar filtros
Borrar filtros

How to select files in a directory

20 visualizaciones (últimos 30 días)
K BV
K BV el 22 de En. de 2013
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 !

Respuesta aceptada

Walter Roberson
Walter Roberson el 22 de En. de 2013
Editada: Walter Roberson el 22 de En. de 2013
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.

Más respuestas (3)

Azzi Abdelmalek
Azzi Abdelmalek el 22 de En. de 2013
Editada: Azzi Abdelmalek el 22 de En. de 2013
d=struct2cell(dir('IM*.*'));
name=d(1,:)
for k=1:numel(name)
file=name{k}
v=str2num(file(6:end))
If v>=25 & v<=52
copyfile(file,'yourfolder')
end

Thorsten
Thorsten el 22 de En. de 2013
% run once for, see if the 'move file' output is ok
% if ok, uncomment the movefile line such that the files are actually moved
listing = dir('IM*.*');
dstdir = './newdir'; % where the selected files should be moved
for i = 1:numel(listing)
filename = d(i).name;
[num elements_matched] = sscanf(filename, 'IM_%d');
if elements_matched && num >= 25 && num <= 52
disp(['move file ' filename ' to ' dstdir '.'])
% movefile(filename, dstdir)
end
end

K BV
K BV el 22 de En. de 2013
Thank you all for your answers ! That helped me extracting the files I need !

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by