how to filter files in matlab
이전 댓글 표시
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
채택된 답변
추가 답변 (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
sandy
2014년 1월 15일
Jos (10584)
2014년 1월 15일
Check the contents of the cell array Files (or c, as you named it).
...
disp(Files{k})
success = copyfile(Files{k}) % copy to current folder
...
sandy
2014년 1월 15일
Image Analyst
2014년 1월 15일
You need to supply two files to copyfile() - a source file and a destination file. So make up two strings.
Jos (10584)
2014년 1월 15일
@Image Analyst: ML changed the behaviour of their function copy file. In the version I am using right now (2011b), the help says:
"[SUCCESS,MESSAGE,MESSAGEID] = COPYFILE(SOURCE) attempts to copy SOURCE to the current directory."
@sandy: see the documentation of copy file for some examples: http://www.mathworks.nl/help/matlab/ref/copyfile.html
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".
카테고리
도움말 센터 및 File Exchange에서 Environment and Settings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!