Loop to Delete Specific Files in Matlab -


i have countless folders need access , delete first 6 files within. currently, have code go folder, , delete each file 1 @ time. can't seem figure out how create loop simplify code.

fn_nifti = 'c: path'; funcdir = 'c: path'/folder'; fnruns = {'folder 1', 'folder 2', 'folder 3', etc..};  i=1:length(fnruns)     delete(fullfile(fn_nifti,funcdir,fnruns{i},'*001.nii'))     delete(fullfile(fn_nifti,funcdir,fnruns{i},'*002.nii'))     delete(fullfile(fn_nifti,funcdir,fnruns{i},'*003.nii'))     delete(fullfile(fn_nifti,funcdir,fnruns{i},'*004.nii'))     delete(fullfile(fn_nifti,funcdir,fnruns{i},'*005.nii'))     delete(fullfile(fn_nifti,funcdir,fnruns{i},'*006.nii')) end 

keep in mind, code above copy/pasted several times. example, have code below such as:

asl_nifti = 'c: path'; funcdir = 'c: path'/folder'; aslruns = {'folder 1', 'folder 2', 'folder 3', etc..};  i=1:length(nwruns)     delete(fullfile(asl_nifti ,funcdir,aslruns {i},'*001.nii'))     delete(fullfile(asl_nifti ,funcdir,aslruns {i},'*002.nii'))     delete(fullfile(asl_nifti ,funcdir,aslruns {i},'*003.nii'))     delete(fullfile(asl_nifti ,funcdir,aslruns {i},'*004.nii'))     delete(fullfile(asl_nifti ,funcdir,aslruns {i},'*005.nii'))     delete(fullfile(asl_nifti ,funcdir,aslruns {i},'*006.nii')) end 

i appreciate help! (with explanation beginner please)

thanks

you start improving loop bit.

for = 1:length(targetfolder)     filenr = 1:6         delete(fullfile(targetfolder{i}, sprintf('*00%d.nii', filenr)));     end end 

the variable targetfolder should contain folders want delete files in. if theese folders selected in special way line them up:

targetfolder = { ...    fullfile(fnrunsrootdir, foldera), ...    fullfile(fnrunsrootdir, folderc), ...    ...    fullfile(alsrunsrootdir, folderx), ...    fullfile(alsrunsrootdir, foldery), ...    ... }; 

if all subfolders in each root folder should picked use dir them

rootdirs = {fnrunsrootdir, alsrunsrootdir, ...}  idir = 1:length(rootdirs)     subdirs = dir(rootdirs{idir});     % pick out valid sub folders     subdir = setdiff({subdir([subdir.isdir]).name}, {'.','..'});     % combine full folder paths     targetfolder = strcat(rootdirs{i}, filesep, subdir);         %%%% loop above goes here %%%%  end 

Comments