MATLAB: How to move files based one whether their name is contained in a spreadsheet

filesfoldersmovefilesortstrcmp

Hello,
I have a folder ('preprocessed') containing about 200 .mat files . Each file is named '[name]_preprocessed.mat' . Here are examples of file names:
34_preprocessed.mat
777_preprocessed.mat
898_preprocessed.mat
HH55_preprocessed.mat
I have a .csv file that contains the [name] of some of these files (i.e. 34; 898).
I would like to move the files whose names are contained in the .csv document to a different folder (the 'test' folder).
Here is what I have for now (I have manually imported the .csv file called "listcsv"):
basepath=['./preprocessed/']
files=dir([basepath '*.mat']);
NS=length(files);
mydir=['./test/']
for s=1:NS
if (strcmp(files(s).name(1),'string found in spreadsheet')) %I think this is where I need help
movefile basepath mydir
end
end
Any help would be greatly appreciated!!

Best Answer

>> cssm
moves the files
where
function cssm
fid = fopen( fullfile('h:/m/cssm/lt_name.csv'), 'r' );
cac = textscan( fid, '%s' );
[~] = fclose( fid );
names = cac{1};
basepath = fullfile('h:/m/cssm/preprocessed/');
mydir = fullfile('h:/m/cssm/test/');
sad = dir( fullfile( basepath, '*.mat' ) );
sad = reshape( sad, 1,[] );
if not( exist( mydir, 'dir' ) == 7 )
[ sts, ~, ~ ] = mkdir( mydir );
assert( sts )
end
for s = sad
str = regexp( s.name, '^[^_]+(?=_preprocessed)', 'once', 'match' );
if ismember( str, names )
movefile( fullfile(basepath,s.name), fullfile(mydir,s.name) );
end
end
end
and lt_name.csv contains
34
898
Related Question