MATLAB: How to rename images from a series of folder

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.

Best Answer

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