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

 Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 16 de En. de 2014

0 votos

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

Más respuestas (1)

Jos (10584)
Jos (10584) el 15 de En. de 2014

0 votos

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 comentarios

sandy
sandy el 15 de En. de 2014
thanks..showing error..
??? Error using ==> copyfile
Argument must contain a string.
Error in ==> sss at 5 success = copyfile(c{k}); % copy to current folder
Jos (10584)
Jos (10584) el 15 de En. de 2014
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
sandy el 15 de En. de 2014
ok thanks jos..its not copying the files which are mentioned inside filestocopy.txt
Image Analyst
Image Analyst el 15 de En. de 2014
You need to supply two files to copyfile() - a source file and a destination file. So make up two strings.
sandy
sandy el 15 de En. de 2014
Editada: sandy el 15 de En. de 2014
is this code correct? Any better codes than this below code?
fid = fopen('filestocopy.txt');
cell_data = textscan(fid, '%s');
fclose(fid);
matrix_data = [cell_data{:}];
path = 'C:\Users\test\MAIN';
listing = dir(fullfile(path, '*.txt'));
mkdir('C:\Users\test\','Filtered Files');
for K = 1 : length(listing)
fname = listing(K).name;
copyfile(fname,'C:\Users\test\Filtered Files\');
end
Jos (10584)
Jos (10584) el 15 de En. de 2014
@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
Image Analyst el 15 de En. de 2014
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".
sandy
sandy el 16 de En. de 2014
Editada: sandy el 16 de En. de 2014
its not working..my code just copying only the .txt files inside MAIN folder .but i need the copy only the file names mentioned inside filestocopy.txt.. showin error as
fid = fopen('filestocopy.txt');
cell_data = textscan(fid, '%s');
fclose(fid);
mat = [cell_data{:}];
path_03 = 'C:\Users\test\Desktop\sample\';
listing = dir(fullfile(path_03, '*.tim'));
mkdir('C:\Users\test\Desktop\sample\','Filtered Files');
a={listing.name};
a=a';
out=strrep(a,'.txt','');
cc=ismember(out,mat);
dd=find(cc);
for k=1:numel(out)
x=out(dd,:);
end
copyfile(x,'C:\Users\test\Desktop\sample\Filtered Files\');
showing error as
??? Error using ==> copyfile
Argument must contain a string.
Error in ==> sss at 18
copyfile(x,'C:\Users\test\Desktop\sample\Filtered
Files\')

Iniciar sesión para comentar.

Categorías

Más información sobre App Building en Centro de ayuda y File Exchange.

Preguntada:

el 15 de En. de 2014

Comentada:

el 16 de En. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by