how to filter files in matlab
26 次查看(过去 30 天)
显示 更早的评论
i have 1000 .txt files with different file name in a folder(MAIN) and i need to filter some files which the names are given in filestocopy.txt. i need to copy those mentioned files and stored in a separate folder..any help..for example..see below..
Inside MAIN folder
SAMPLE_20120807_1816.TXT
SAMPLE_20120807_1916.TXT
SAMPLE_20121207_1311.TXT
SAMPLE_20121307_1816.TXT
SAMPLE_20121322_1902.TXT
SAMPLE_20121412_1351.TXT
Inside filestocopy.txt
SAMPLE_20120807_1816
SAMPLE_20121207_1311
SAMPLE_20121412_1351
0 个评论
采纳的回答
Azzi Abdelmalek
2014-1-16
files={'SAMPLE_20120807_1816.TXT'
'SAMPLE_20120807_1916.TXT'
'SAMPLE_20121207_1311.TXT'
'SAMPLE_20121307_1816.TXT'
'SAMPLE_20121322_1902.TXT'
'SAMPLE_20121412_1351.TXT'}
files1=strrep(files,'.TXT','')
c=importdata('filename1.txt')
idx=ismember(files1,strtrim(c))
out=files(idx)
destintion='E:/'
for k=1:numel(out)
copyfile(out{k},destination)
end
更多回答(1 个)
Jos (10584)
2014-1-15
Here is some code to get you started:
CopyFolder = './MyFolder' ;
fid = fopen('files2copy.txt');
Files = textscan(fid, '%s');
fclose(fid);
CurrentFolder = pwd ;
cd (CopyFolder)
for k=1:numel(Files)
success = copyfile(Files{k}) % copy to current folder
if ~success,
disp([Error in copying file: ' Files{k})]) ;
break
end
end
cd (CurrentFolder) % back to original folder
8 个评论
Image Analyst
2014-1-15
Sandy, you're not accepting the success return arguments. It's more robust to do that, like Jos's code showed. Anyway, does it work? If it does, mark the Answer as "Accepted".
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Low-Level File I/O 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!