网站关键词和描述纯注册app拉新平台
2026/4/6 7:42:12 网站建设 项目流程
网站关键词和描述,纯注册app拉新平台,厦门电商网站开发,郏县住房和城乡建设局网站一、简介 因为工程需要#xff0c;现在需要对文件夹的名称进行批量修改。原本的文件夹名称是从随机数字开始排序的#xff0c;如图所示#xff1a; 现在需要将其改为从1开始排序。 在这个过程中出现了一些问题#xff0c;在此记录一下。 参考链接#xff1a; link1 link…一、简介因为工程需要现在需要对文件夹的名称进行批量修改。原本的文件夹名称是从随机数字开始排序的如图所示现在需要将其改为从1开始排序。在这个过程中出现了一些问题在此记录一下。参考链接link1link2二、解决过程修改文件夹名称的代码参考link1。代码如下close all;clear all;folder_pathE:\DataSet\。。。\Images;% 手动打开要修改名称的文件夹的上一层文件夹folderdir(folder_path);oldnamecell(length(folder)-2,1);forii3:length(folder)oldname{ii-2}folder(ii).name;endsort_nat_namesort_nat({oldname});% 提取出要修改文件夹的名称newnamecell(length(oldname),1);forii1:length(oldname)aii;newname{ii}num2str(a);ifnewname{ii}oldname{ii}continueend% 新的文件夹名称movefile([folder_path\oldname{ii}],[folder_path\newname{ii}])% 利用movefile函数进行修改end三、问题记录一问题1问题描述使用dir函数读取的文件名顺序与实际顺序不符文件夹的名称显然是按照文件顺序来修改的读取顺序与实际顺序不符则修改的也会出问题。实际文件顺序如图所示读取后的名称顺序为原因在于dir读取的文件顺序不是按照十进制排序的。解决方案参考link2我们中间添加一个 sort_nat() 函数对files.name 进行排序。sort_nat() 函数如下所示%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 dont 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: dmschwarzieee*org, dmschwarzurgrad*rochester*edu% Real_email regexprep(Email,{,*},{,.})% Set default value for mode if necessary.ifnargin2modeascend;end% Make sure mode is either ascend or descend.modesstrcmpi(mode,{ascend,descend});is_descendmodes(2);if~any(modes)error(sort_nat:sortDirection,...sorting direction must be ascend or descend.)end% Replace runs of digits with 0.c2regexprep(c,\d,0);% Compute char version of c2 and locations of zeros.s1char(c2);zs10;% 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_strlength(c);max_lensize(s1,2);num_valNaN(num_str,max_len);num_digNaN(num_str,max_len);fori1:num_strnum_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.activecolsreshape(find(~all(isnan(num_val))),1,[]);nlength(activecols);% Compute which columns in the composite matrix get the numbers.numcolsactivecols(1:2:2*n);% Compute which columns in the composite matrix get the number of digits.ndigcolsnumcols1;% Compute which columns in the composite matrix get chars.charcolstrue(1,max_len2*n);charcols(numcols)false;charcols(ndigcols)false;% Create and fill composite matrix, comp.compzeros(num_str,max_len2*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);ifis_descend indexindex(end:-1:1);endindexreshape(index,size(c));csc(index);二问题2问题描述错误使用 regexprep 所有元胞都必须为字符行向量。这里我读取的 oldname 为列向量因此在运行 sort_nat() 函数时出现了错误。解决方案这里我先将 oldname 转置为行向量排序完成后再将其变为列向量。四、整体代码主函数整体代码如下close all;clear all;folder_pathE:\DataSet\。。。\Images;% 手动打开要修改名称的文件夹的上一层文件夹folderdir(folder_path);oldname0cell(length(folder)-2,1);forii3:length(folder)oldname0{ii-2}folder(ii).name;endoldname_toldname0;sort_nat_namesort_nat(oldname_t);oldnamesort_nat_name;% 提取出要修改文件夹的名称newnamecell(length(oldname),1);forii1:length(oldname)aii;newname{ii}num2str(a);ifnewname{ii}oldname{ii}continueend% 新的文件夹名称movefile([folder_path\oldname{ii}],[folder_path\newname{ii}])% 利用movefile函数进行修改end

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询