Matlab批量修改文件夹的名称

一、简介

因为工程需要,现在需要对文件夹的名称进行批量修改。原本的文件夹名称是从随机数字开始排序的,如图所示:

现在需要将其改为从1开始排序。

在这个过程中出现了一些问题,在此记录一下。

参考链接:
link1
link2

二、解决过程

修改文件夹名称的代码参考link1。代码如下:

matlab 复制代码
close all;
clear all;

folder_path = 'E:\DataSet\。。。\Images'; 

% 手动打开要修改名称的文件夹的上一层文件夹
folder=dir(folder_path);
oldname=cell(length(folder)-2,1);
for ii=3:length(folder)
   oldname{ii-2}=folder(ii).name;
end

sort_nat_name=sort_nat({oldname});

% 提取出要修改文件夹的名称
newname=cell(length(oldname),1);
for ii=1:length(oldname)
   a=ii;
   newname{ii}=num2str(a);

   if newname{ii} == oldname{ii}
       continue
   end
   % 新的文件夹名称 
   movefile([folder_path '\' oldname{ii}],[folder_path '\' newname{ii}])

   % 利用movefile函数进行修改
end

三、问题记录

(一)问题1

问题描述:使用dir函数读取的文件名顺序与实际顺序不符

文件夹的名称显然是按照文件顺序来修改的,读取顺序与实际顺序不符则修改的也会出问题。

实际文件顺序如图所示:

读取后的名称顺序为:

原因在于dir读取的文件顺序不是按照十进制排序的。
解决方案:

参考link2,我们中间添加一个 sort_nat() 函数,对files.name 进行排序。

sort_nat() 函数如下所示:

matlab 复制代码
%sort_nat具体内容
function [cs,index] = sort_nat(c,mode)
%sort_nat: Natural order sort of cell array of strings.
% usage:  [S,INDEX] = sort_nat(C)
%
% where,
%    C is a cell array (vector) of strings to be sorted.
%    S is C, sorted in natural order.
%    INDEX is the sort order such that S = C(INDEX);
%
% Natural order sorting sorts strings containing digits in a way such that
% the numerical value of the digits is taken into account.  It is
% especially useful for sorting file names containing index numbers with
% different numbers of digits.  Often, people will use leading zeros to get
% the right sort order, but with this function you don't have to do that.
% For example, if C = {'file1.txt','file2.txt','file10.txt'}, a normal sort
% will give you
%
%       {'file1.txt'  'file10.txt'  'file2.txt'}
%
% whereas, sort_nat will give you
%
%       {'file1.txt'  'file2.txt'  'file10.txt'}
%
% See also: sort

% Version: 1.4, 22 January 2011
% Author:  Douglas M. Schwarz
% Email:   dmschwarz=ieee*org, dmschwarz=urgrad*rochester*edu
% Real_email = regexprep(Email,{'=','*'},{'@','.'})


% Set default value for mode if necessary.
if nargin < 2
    mode = 'ascend';
end

% Make sure mode is either 'ascend' or 'descend'.
modes = strcmpi(mode,{'ascend','descend'});
is_descend = modes(2);
if ~any(modes)
    error('sort_nat:sortDirection',...
        'sorting direction must be ''ascend'' or ''descend''.')
end

% Replace runs of digits with '0'.
c2 = regexprep(c,'\d+','0');

% Compute char version of c2 and locations of zeros.
s1 = char(c2);
z = s1 == '0';

% Extract the runs of digits and their start and end indices.
[digruns,first,last] = regexp(c,'\d+','match','start','end');

% Create matrix of numerical values of runs of digits and a matrix of the
% number of digits in each run.
num_str = length(c);
max_len = size(s1,2);
num_val = NaN(num_str,max_len);
num_dig = NaN(num_str,max_len);
for i = 1:num_str
    num_val(i,z(i,:)) = sscanf(sprintf('%s ',digruns{i}{:}),'%f');
    num_dig(i,z(i,:)) = last{i} - first{i} + 1;
end

% Find columns that have at least one non-NaN.  Make sure activecols is a
% 1-by-n vector even if n = 0.
activecols = reshape(find(~all(isnan(num_val))),1,[]);
n = length(activecols);

% Compute which columns in the composite matrix get the numbers.
numcols = activecols + (1:2:2*n);

% Compute which columns in the composite matrix get the number of digits.
ndigcols = numcols + 1;

% Compute which columns in the composite matrix get chars.
charcols = true(1,max_len + 2*n);
charcols(numcols) = false;
charcols(ndigcols) = false;

% Create and fill composite matrix, comp.
comp = zeros(num_str,max_len + 2*n);
comp(:,charcols) = double(s1);
comp(:,numcols) = num_val(:,activecols);
comp(:,ndigcols) = num_dig(:,activecols);

% Sort rows of composite matrix and use index to sort c in ascending or
% descending order, depending on mode.
[unused,index] = sortrows(comp);
if is_descend
    index = index(end:-1:1);
end
index = reshape(index,size(c));
cs = c(index);

(二)问题2

问题描述:

bash 复制代码
错误使用 regexprep
所有元胞都必须为字符行向量。

这里我读取的 oldname 为列向量,因此在运行 sort_nat() 函数时出现了错误。

解决方案:

这里我先将 oldname 转置为行向量,排序完成后再将其变为列向量。

四、整体代码

主函数整体代码如下:

matlab 复制代码
close all;
clear all;

folder_path = 'E:\DataSet\。。。\Images'; 

% 手动打开要修改名称的文件夹的上一层文件夹
folder=dir(folder_path);
oldname0=cell(length(folder)-2,1);
for ii=3:length(folder)
   oldname0{ii-2}=folder(ii).name;
end

oldname_t = oldname0';

sort_nat_name=sort_nat(oldname_t);

oldname = sort_nat_name';

% 提取出要修改文件夹的名称
newname=cell(length(oldname),1);
for ii=1:length(oldname)
   a=ii;
   newname{ii}=num2str(a);

   if newname{ii} == oldname{ii}
       continue
   end
   % 新的文件夹名称 
   movefile([folder_path '\' oldname{ii}],[folder_path '\' newname{ii}])

   % 利用movefile函数进行修改
end
相关推荐
gihigo199810 小时前
距离角度解耦法的MIMO-OFDM雷达波束形成及优化MATLAB实现
开发语言·算法·matlab
机器学习之心10 小时前
LightGBM多变量回归区间预测(点预测 + 区间预测),MATLAB代码
matlab·回归·区间预测
jghhh0111 小时前
运动图像的运动轨迹检测与特征点跟踪MATLAB实现
人工智能·计算机视觉·matlab
fengfuyao98511 小时前
一个改进的MATLAB CVA(Change Vector Analysis)变化检测程序
前端·算法·matlab
机器学习之心HML12 小时前
考虑气象因素的贝叶斯优化短期电力负荷预测研究,MATLAB代码
开发语言·matlab
机器学习之心14 小时前
多工况车速数据集训练GRU门控循环单元用于车速预测,输出未来多个时间步车速,MATLAB代码
深度学习·matlab·gru·车速预测
简简单单做算法16 小时前
基于hough变换和线段分类算法的金属冲孔板裂痕检测matlab仿真
matlab·hough变换·线段分类·金属冲孔板·裂痕检测
enmouhuadou1 天前
快速运行matlab仿真方法
开发语言·matlab
神仙别闹1 天前
基于MATLAB实现(GUI)汽车出入库识别系统
开发语言·matlab·汽车
hoiii1872 天前
MATLAB模拟ADS-B数据解码与信号处理整体流程
数据结构·matlab·信号处理