【问题标题】:Matlab: Renaming files in a folder sequentiallyMatlab:按顺序重命名文件夹中的文件
【发布时间】:2014-08-23 05:39:00
【问题描述】:

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

文件1.TIF,文件2.TIF ....文件100.TIF

MatLab 可以自动将它们重命名为:

file_0001.TIF, file_0002.TIF, ....file_0100.TIF?

【问题讨论】:

  • 我猜如果你在 for 循环中加载它们并用新名称保存它们,你就可以做到。我不知道有没有办法不打开和关闭它们
  • 查看movefile。我想知道是否有无循环单元阵列方法,或者可能使用cellfun,我怀疑这会带来任何性能改进,最多可能只是优雅。
  • 感谢#Ander Biguri 和#Divakar

标签: matlab file renaming


【解决方案1】:

无循环方法 -

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.TIFfile28.TIFfile29.TIF 等的情况,您想将它们分别重命名为file0001.TIFfile0002.TIFfile0003.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

【讨论】:

  • 我知道必须有一种方法可以不使用循环。
  • @excaza 是的,我猜arrayfuncellfun 只会带来“优雅”,但我怀疑是否会有性能提升。
  • 非常感谢。如果我的文件夹中有以下文件怎么样:file27.TIF、file28.TIF、file29.TIF。我可以从 0001 开始重命名它们,即将它们重命名为:file0001.TIF、file0002.TIF、file0003.TIF
  • @Mosawi 是的,我想是的。在你拥有file_ID 之后,执行此操作 - file_ID = file_ID - 26str_zeros = arrayfun(@(t) repmat('0',1,t), 4-cellfun(@numel,file_ID),'uni',0),最后是 new_filename = strcat('file',str_zeros,file_ID,'.TIF')
  • 对于 (file_ID - 6),我收到错误“未定义函数 'minus' 用于类型 'cell' 的输入参数”。知道如何解决这个问题吗?
【解决方案2】:

一种更稳健的方法:

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 以将东西变成更易于管理的格式。它还允许我们对一些代码进行矢量化处理。

【讨论】:

    猜你喜欢
    • 2015-01-24
    • 2016-06-28
    • 1970-01-01
    • 2017-04-10
    • 2019-07-15
    • 2020-10-20
    • 1970-01-01
    • 2014-04-22
    • 2015-11-24
    相关资源
    最近更新 更多