您的位置:首页 > 编程语言 > MATLAB

how to change file names in matlab

2017-01-04 01:22 411 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: