news 2026/4/18 9:27:20

Matlab批量修改文件夹的名称

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Matlab批量修改文件夹的名称

一、简介

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

现在需要将其改为从1开始排序。
在这个过程中出现了一些问题,在此记录一下。
参考链接:
link1
link2

二、解决过程

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

close all;clear all;folder_path='E:\DataSet\。。。\Images';% 手动打开要修改名称的文件夹的上一层文件夹folder=dir(folder_path);oldname=cell(length(folder)-2,1);forii=3:length(folder)oldname{ii-2}=folder(ii).name;endsort_nat_name=sort_nat({oldname});% 提取出要修改文件夹的名称newname=cell(length(oldname),1);forii=1:length(oldname)a=ii;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 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.ifnargin<2mode='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);fori=1: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.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);ifis_descend index=index(end:-1:1);endindex=reshape(index,size(c));cs=c(index);

(二)问题2

问题描述:

错误使用 regexprep 所有元胞都必须为字符行向量。

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

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

四、整体代码

主函数整体代码如下:

close all;clear all;folder_path='E:\DataSet\。。。\Images';% 手动打开要修改名称的文件夹的上一层文件夹folder=dir(folder_path);oldname0=cell(length(folder)-2,1);forii=3:length(folder)oldname0{ii-2}=folder(ii).name;endoldname_t=oldname0';sort_nat_name=sort_nat(oldname_t);oldname=sort_nat_name';% 提取出要修改文件夹的名称newname=cell(length(oldname),1);forii=1:length(oldname)a=ii;newname{ii}=num2str(a);ifnewname{ii}==oldname{ii}continueend% 新的文件夹名称movefile([folder_path'\'oldname{ii}],[folder_path'\'newname{ii}])% 利用movefile函数进行修改end
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/18 8:31:58

Onekey:Steam游戏清单高效管理与数据备份全攻略

Onekey&#xff1a;Steam游戏清单高效管理与数据备份全攻略 【免费下载链接】Onekey Onekey Steam Depot Manifest Downloader 项目地址: https://gitcode.com/gh_mirrors/one/Onekey Onekey是一款专为Steam平台设计的Depot Manifest下载工具&#xff0c;核心功能包括游…

作者头像 李华
网站建设 2026/4/18 8:18:31

Agentic AI农业项目:提示工程架构师如何进行系统设计?

Agentic AI农业项目&#xff1a;提示工程架构师的系统设计指南 一、引言&#xff1a;当AI成为农民的“智能伙伴” 1.1 一个真实的农业痛点&#xff1a;暴雨后的绝望 2023年夏天&#xff0c;河南周口的玉米种植户王大哥遭遇了一场噩梦——连续3天的暴雨过后&#xff0c;地里的玉…

作者头像 李华
网站建设 2026/4/18 7:37:00

Z-Image Turbo自主部署:企业级安全绘图环境搭建

Z-Image Turbo自主部署&#xff1a;企业级安全绘图环境搭建 1. 为什么需要本地部署一个“极速画板” 你有没有遇到过这些情况&#xff1a; 在线AI绘图平台生成一张图要排队5分钟&#xff0c;导出还带水印&#xff1b;企业设计团队想批量生成产品概念图&#xff0c;但担心提示…

作者头像 李华
网站建设 2026/4/16 10:51:41

手把手教学:基于Streamlit的DeepSeek-R1聊天界面开发

手把手教学&#xff1a;基于Streamlit的DeepSeek-R1聊天界面开发 1. 为什么选Streamlit做这个聊天界面&#xff1f; 1.1 你可能正面临这些实际问题 你刚下载好 DeepSeek-R1-Distill-Qwen-1.5B 这个轻量又聪明的模型&#xff0c;但卡在了最后一步——怎么让它真正“用起来”&…

作者头像 李华
网站建设 2026/4/18 9:22:19

【绝密工程笔记】:某九章光量子团队如何用C语言实现128通道并行微波脉冲生成(时钟抖动<1.7ps,附FPGA-CPU协同调度算法)

第一章&#xff1a;C语言量子芯片控制接口开发在超导量子处理器的实际工程部署中&#xff0c;C语言因其确定性执行、内存可控性与实时中断响应能力&#xff0c;成为底层硬件控制接口的首选实现语言。本章聚焦于构建一个轻量、可嵌入、符合QISKit-RT扩展规范的C语言控制接口层&a…

作者头像 李华
网站建设 2026/4/17 19:21:40

轻量级图像工具ImageGlass:重新定义高效图像浏览体验

轻量级图像工具ImageGlass&#xff1a;重新定义高效图像浏览体验 【免费下载链接】ImageGlass &#x1f3de; A lightweight, versatile image viewer 项目地址: https://gitcode.com/gh_mirrors/im/ImageGlass 在数字内容爆炸的时代&#xff0c;高效图像浏览已成为专业…

作者头像 李华