Matlab:按顺序重命名文件夹中的文件

Matlab:按顺序重命名文件夹中的文件,matlab,file,renaming,Matlab,File,Renaming,如果文件夹C:\test\中有以下文件: file1.TIF,file2.TIF。。。。file100.TIF MatLab能否自动将其重命名为: 文件_0001.TIF,文件_0002.TIF。。。。文件_0100.TIF?一种稍微稳健的方法: dirlist = dir(fullfile(mypath,'*.TIF')); fullnames = {dirlist.name}; % Get rid of one layer of cell array-ness [~,fnames,~] =

如果文件夹C:\test\中有以下文件:

file1.TIF,file2.TIF。。。。file100.TIF

MatLab能否自动将其重命名为:


文件_0001.TIF,文件_0002.TIF。。。。文件_0100.TIF?

一种稍微稳健的方法:

dirlist = dir(fullfile(mypath,'*.TIF'));
fullnames = {dirlist.name}; % Get rid of one layer of cell array-ness

[~,fnames,~] = cellfun(@fileparts,fullnames,'UniformOutput',false); % Create cell array of the file names from the output of dir()
fnums = cellfun(@str2double,regexprep(fnames,'[^0-9]','')); % Delete any character that isn't a number, returns it as a vector of doubles
fnames = regexprep(fnames,'[0-9]',''); % Delete any character that is a number

for ii = 1:length(dirlist)
    newname = sprintf('%s_%04d.TIF',fnames{ii},fnums(ii)); % Create new file name

    oldfile = fullfile(mypath,dirlist(ii).name); % Generate full path to old file
    newfile = fullfile(mypath,newname);          % Generate full path to new file

    movefile(oldfile, newfile); % Rename the files
end

尽管这将容纳任何长度的文件名,但它确实假设文件名中除了末尾的计数器之外没有其他数字。MATLAB喜欢将东西放入嵌套的单元格数组中,因此我在几个地方合并了
cellfun
,将东西放入更易于管理的格式中。它还允许我们对一些代码进行矢量化。

一种更稳健的方法:

dirlist = dir(fullfile(mypath,'*.TIF'));
fullnames = {dirlist.name}; % Get rid of one layer of cell array-ness

[~,fnames,~] = cellfun(@fileparts,fullnames,'UniformOutput',false); % Create cell array of the file names from the output of dir()
fnums = cellfun(@str2double,regexprep(fnames,'[^0-9]','')); % Delete any character that isn't a number, returns it as a vector of doubles
fnames = regexprep(fnames,'[0-9]',''); % Delete any character that is a number

for ii = 1:length(dirlist)
    newname = sprintf('%s_%04d.TIF',fnames{ii},fnums(ii)); % Create new file name

    oldfile = fullfile(mypath,dirlist(ii).name); % Generate full path to old file
    newfile = fullfile(mypath,newname);          % Generate full path to new file

    movefile(oldfile, newfile); % Rename the files
end
尽管这将容纳任何长度的文件名,但它确实假设文件名中除了末尾的计数器之外没有其他数字。MATLAB喜欢将东西放入嵌套的单元格数组中,因此我在几个地方合并了
cellfun
,将东西放入更易于管理的格式中。它还允许我们对一些代码进行矢量化。

无循环方法-

directory  = 'C:\test\'; %//' Directory where TIFF images are present
filePattern = fullfile(directory, 'file*.tif'); %//' files pattern with absolute paths
old_filename = cellstr(ls(filePattern)) %// Get the filenames
file_ID = strrep(strrep(old_filename,'file',''),'.TIF','') %// Get numbers associated with each file
str_zeros = arrayfun(@(t) repmat('0',1,t), 5-cellfun(@numel,file_ID),'uni',0) %// Get zeros string to be pre-appended to each filename
new_filename = strcat('file_',str_zeros,file_ID,'.TIF') %// Generate new filenames
cellfun(@(m1,m2) movefile(m1,m2),fullfile(directory,old_filename),fullfile(directory,new_filename)) %// Finally rename files with the absolute paths
编辑1:

如果您的文件名分别为
file27.TIF
file28.TIF
file29.TIF
等,并且希望将其重命名为
file0001.TIF
file0002.TIF
file0003.TIF
等,请尝试以下操作-

directory  = 'C:\test\'; %//' Directory where TIFF images are present
filePattern = fullfile(directory, 'file*.tif'); %//' files pattern with absolute paths
old_filename = cellstr(ls(filePattern)) %// Get the filenames
file_ID = strrep(strrep(old_filename,'file',''),'.TIF','') %// Get numbers associated with each file

file_ID_doublearr = str2double(file_ID)
file_ID_doublearr = file_ID_doublearr - min(file_ID_doublearr)+1

file_ID = strtrim(cellstr(num2str(file_ID_doublearr)))

str_zeros = arrayfun(@(t) repmat('0',1,t), 4-cellfun(@numel,file_ID),'uni',0) %// Get zeros string to be pre-appended to each filename
new_filename = strcat('file',str_zeros,file_ID,'.TIF') %// Generate new filenames
cellfun(@(m1,m2) movefile(m1,m2),fullfile(directory,old_filename),fullfile(directory,new_filename)) %// Finally rename files with the absolute paths
无回路法-

directory  = 'C:\test\'; %//' Directory where TIFF images are present
filePattern = fullfile(directory, 'file*.tif'); %//' files pattern with absolute paths
old_filename = cellstr(ls(filePattern)) %// Get the filenames
file_ID = strrep(strrep(old_filename,'file',''),'.TIF','') %// Get numbers associated with each file
str_zeros = arrayfun(@(t) repmat('0',1,t), 5-cellfun(@numel,file_ID),'uni',0) %// Get zeros string to be pre-appended to each filename
new_filename = strcat('file_',str_zeros,file_ID,'.TIF') %// Generate new filenames
cellfun(@(m1,m2) movefile(m1,m2),fullfile(directory,old_filename),fullfile(directory,new_filename)) %// Finally rename files with the absolute paths
编辑1:

如果您的文件名分别为
file27.TIF
file28.TIF
file29.TIF
等,并且希望将其重命名为
file0001.TIF
file0002.TIF
file0003.TIF
等,请尝试以下操作-

directory  = 'C:\test\'; %//' Directory where TIFF images are present
filePattern = fullfile(directory, 'file*.tif'); %//' files pattern with absolute paths
old_filename = cellstr(ls(filePattern)) %// Get the filenames
file_ID = strrep(strrep(old_filename,'file',''),'.TIF','') %// Get numbers associated with each file

file_ID_doublearr = str2double(file_ID)
file_ID_doublearr = file_ID_doublearr - min(file_ID_doublearr)+1

file_ID = strtrim(cellstr(num2str(file_ID_doublearr)))

str_zeros = arrayfun(@(t) repmat('0',1,t), 4-cellfun(@numel,file_ID),'uni',0) %// Get zeros string to be pre-appended to each filename
new_filename = strcat('file',str_zeros,file_ID,'.TIF') %// Generate new filenames
cellfun(@(m1,m2) movefile(m1,m2),fullfile(directory,old_filename),fullfile(directory,new_filename)) %// Finally rename files with the absolute paths

我想如果你将它们加载到for循环中,并用新名称保存它们,你就可以做到。我不知道是否有不打开和关闭它们的方法查看
movefile
。我想知道是否有一种无循环的单元数组方法,或者使用
cellfun
,我怀疑这会带来任何性能改进,最多可能只是优雅而已。谢谢#Ander Biguri和#DivakarI猜测如果你将它们加载到for循环中并用新名称保存它们,你就可以做到。我不知道是否有不打开和关闭它们的方法查看
movefile
。我想知道是否有一种无环单元阵列的方法,或者可能有
cellfun
,我怀疑这会带来任何性能改进,最多可能只是优雅而已。谢谢#Ander Biguri,而且#DivakarI知道必须有一种不需要循环的方法。@excaza是的,我猜
arrayfun
cellfun
只会带来“优雅”,但我怀疑是否会有性能改进。非常感谢。如果我在文件夹中有以下文件,怎么样:file27.TIF、file28.TIF、file29.TIF。我可以从0001开始重命名它们,即重命名为:file0001.TIF、file0002.TIF、file0003。TIF@Mosawi是的,我想是的。拥有
文件ID
后,执行此操作-
文件ID=file\u ID-26
str\u zeros=arrayfun(@(t)repmat('0',1,t),4-cellfun(@numel,file\u ID),'uni',0)
最后
新文件名=strcat('file',str zeros,file\u ID',.TIF')对于(文件ID-6),我得到错误“对于类型为“cell”的输入参数,“未定义的函数‘减号’。知道如何修复吗?我知道必须有一种方法在没有循环的情况下完成它。@excaza是的,我猜
arrayfun
cellfun
只带来了“优雅”“说到桌面,我怀疑是否会有绩效改进。非常感谢。如果我在文件夹中有以下文件,怎么样:file27.TIF、file28.TIF、file29.TIF。我可以从0001开始重命名它们,即重命名为:file0001.TIF、file0002.TIF、file0003。TIF@Mosawi是的,我想是的。拥有
文件ID
后,执行此操作-
文件ID=file\u ID-26
str\u zeros=arrayfun(@(t)repmat('0',1,t),4-cellfun(@numel,file\u ID),'uni',0)
最后
新文件名=strcat('file',str zeros,file\u ID',.TIF')对于(文件ID-6),我得到错误对于“cell”类型的输入参数,未定义函数“减号”。你知道如何解决这个问题吗?