How to rename images from a series of folder

1 Ansicht (letzte 30 Tage)
Md
Md am 28 Mär. 2014
Kommentiert: Walter Roberson am 24 Dez. 2018
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.

Akzeptierte Antwort

Marta Salas
Marta Salas am 28 Mär. 2014
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 Kommentare
Le Dung
Le Dung am 24 Dez. 2018
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 am 24 Dez. 2018
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'

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Search Path finden Sie in Help Center und File Exchange

Tags

Noch keine Tags eingegeben.

Community Treasure Hunt

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

Start Hunting!

Translated by