フィルターのクリア

How to select files in a directory

20 ビュー (過去 30 日間)
K BV
K BV 2013 年 1 月 22 日
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 !

採用された回答

Walter Roberson
Walter Roberson 2013 年 1 月 22 日
編集済み: Walter Roberson 2013 年 1 月 22 日
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.

その他の回答 (3 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 1 月 22 日
編集済み: Azzi Abdelmalek 2013 年 1 月 22 日
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 2013 年 1 月 22 日
% 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 2013 年 1 月 22 日
Thank you all for your answers ! That helped me extracting the files I need !

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by