How to rename images from a series of folder

1 次查看(过去 30 天)
Hi All, I worte a code to rename the images in the folders. This is my code.
mainDirectory = 'C:\Users\md\Desktop\NewFolder';
subDirectory = dir([mainDirectory '/N*']);
for m = 1 : length(subDirectory)
a=char('mainDirectory '\' subDirectory(m))');
subFolder = dir(a,'\*.tif');
fileNames = {subFolder.name};
for iFile = 1 : numel( subFolder )
newName = fullfile(subDirectory, sprintf( 'D_1_20%2d.tif',(14-iFile) ) );
movefile( fullfile(subDirectory, fileNames{ iFile }), newName );
end
end
when I run the code, it is saying that too many input arguments. I don't know what I am doing wrong. also there is an error in this line
subFolder = dir(a,'\*.tif');
Thanks in advance.

采纳的回答

Marta Salas
Marta Salas 2014-3-28
mainDirectory = 'C:\Users\md\Desktop\NewFolder';
subDirectory = dir([mainDirectory '/N*']);
for m = 1 : length(subDirectory)
subFolder = dir(fullfile(mainDirectory, subDirectory(m).name,'*.tif'));
fileNames = {subFolder.name};
for iFile = 1 : numel( subFolder )
newName = fullfile(mainDirectory, subDirectory(m).name, sprintf( 'D_1_20%2d.tif',(14-iFile) ) );
movefile( fullfile(mainDirectory, subDirectory(m).name, fileNames{ iFile }), newName );
end
end
  3 个评论
Le Dung
Le Dung 2018-12-24
what is the meaning of '/N*' in the command line:
subDirectory = dir([mainDirectory '/N*']);
When i use the cmmand line, matlab return:
0 x 1 struc with 5 fields
but, when i dont use '/N*', matlab return:
11x1 struc with 5 fields
Is '/N*' for folders and not take into account is for files?
Could you explain to me ?
Walter Roberson
Walter Roberson 2018-12-24
The original poster in 2014, md, happened to be interested in only the subfolders whose names began with the letter N. It is not some kind of special command switch: it is just part of the name.
Another way that it could have been coded would be
subDirectory = dir( fullfile(mainDirectory, 'N*') );
You might have been misled by the fact that a / was used when you expected to see \ for directory names. It turns out that internally, MS DOS has always used / as the directory separator, but that way way back for compatibility with CP/M command line structure that used / as the way to indicate command options, the shell used \ to indicate directory boundaries. But that was only for presentation purposes, and internally it uses / . Therefore in MS Windows, 'C:\Users\md\Desktop\NewFolder' is the same as 'C:/Users/md/Desktop/NewFolder'

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Search Path 的更多信息

标签

尚未输入任何标签。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by