how to change file names in matlab

Assuming we have some files in a fold named 'photo', some file names are ended with '.gif', some of them are not. now our goal is to add '.gif' suffix to all the files except the '.gif' files they were before.


 

pics = dir('photo');%list all the files in folder photo
name = extractfield(pics, 'name');% extract name fields into a cell array
index = ~ismember(name,[ {'.'},{'..'}]);%exclude current directory and parent directory
name = name(index);

gif = regexp(name, '.gif$');%find out the original gif files
index_nongif = cellfun(@isempty, gif);% filter out gif files
name = name(index_nongif);


before_name = cellfun(@strcat, repmat({'photo\'},size(name)), name, 'UniformOutput', 0);% add photo prefix
after_name = cellfun(@strcat, before_name, repmat({'.gif'},size(name)), 'UniformOutput', 0);% add .gif suffix


cellfun(@movefile, before_name, after_name)% change file names

你可能感兴趣的:(MATLAB)