在matlab中重命名图像文件名

3 matlab rename image file

我从互联网站点加载10,000个图像文件并将其保存在文件夹中以在我的项目(图像检索系统)中使用它,现在我需要重命名图像文件,如(image1,image2,image3,.... image10000),任何人都可以帮助我......我想告诉你我在工作中使用了matlab

谢谢

gno*_*ice 14

您需要记住的一件事是文件名的数字部分的格式究竟如何,因为这有时会影响目录中文件的顺序.例如,使用上面给出的命名约定有时会产生如下排序顺序:

image1.jpg
image10.jpg
image11.jpg
image2.jpg
image3.jpg
...
Run Code Online (Sandbox Code Playgroud)

这通常不是你想要的.如果您使用零填充数字达到最大数字大小(在您的情况下为5位数),则应在目录中更好地维护排序顺序:

image00001.jpg
image00002.jpg
image00003.jpg
....
Run Code Online (Sandbox Code Playgroud)

要创建这样的文件名,可以使用SPRINTF函数.下面是一些示例代码,它以这种方式重命名目录中的所有.jpg文件:

dirData = dir('*.jpg');         %# Get the selected file data
fileNames = {dirData.name};     %# Create a cell array of file names
for iFile = 1:numel(fileNames)  %# Loop over the file names
  newName = sprintf('image%05d.jpg',iFile);  %# Make the new name
  movefile(fileNames{iFile},newName);        %# Rename the file
end
Run Code Online (Sandbox Code Playgroud)

上面的代码也使用了DIRMOVEFILE函数(如其他答案中所述).