MATLAB: How to rename images using a loop

imagelooprename

Hello. I have 150 images (images_0, images_1, images_2, ……, images_140) which are saved in 4 folders called : dataset_1, dataset_2, dataset_3 and dataset_4.
I use this code to rename images
mainDirectory = 'C:\Users\Desktop\data';
subDirectory = dir([mainDirectory '/dataset_*']);
for m = 1 : length(subDirectory)
subFolder = dir(fullfile(mainDirectory, subDirectory(m).name,'*.png'));
fileNames = {subFolder.name};
for iFile = 1 : numel( subFolder )
newName = fullfile(mainDirectory, subDirectory(m).name, sprintf('%00d.png',(iFile) ) );
movefile( fullfile(mainDirectory, subDirectory(m).name, fileNames{ iFile }), newName );
end
end
This code works well, but I'm new in MATLAB and I want to change the newName as follows : (number of the dataset)_(name of the image)
For example : 1_images_0, 1_images_1, 2_images_0, 2_images_1, …
Please, any idea how can I change the newName to rename the images? Please help me and thanks in advance.

Best Answer

This will do it:
% Start with a folder and get a list of all subfolders.
% Finds and prints names of all files in
% that folder and all of its subfolders.
% Similar to imageSet() function in the Computer Vision System Toolbox: http://www.mathworks.com/help/vision/ref/imageset-class.html
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
% Define a starting folder.
start_path = pwd; % %'C:\Users\Desktop\data';
if ~exist(start_path, 'dir')
start_path = matlabroot;
end
% Ask user to confirm or change.
uiwait(msgbox('Pick a starting folder on the next window that will come up.'));
topLevelFolder = uigetdir(start_path);
if topLevelFolder == 0
return;
end
% Get list of all subfolders.
allSubFolders = genpath(topLevelFolder);
% Parse into a cell array.
remain = allSubFolders;
listOfFolderNames = {};
while true
[singleSubFolder, remain] = strtok(remain, ';');
if isempty(singleSubFolder)
break;
end
listOfFolderNames = [listOfFolderNames singleSubFolder];
end
numberOfFolders = length(listOfFolderNames)
% Process all image files in those folders.
for k = 1 : numberOfFolders
% Get this folder and print it out.
thisFolder = listOfFolderNames{k};
fprintf('Processing folder %s\n', thisFolder);
% Get ALL files in this folder.
filePattern = sprintf('%s/*.*', thisFolder);
baseFileNames = dir(filePattern);
numberOfImageFiles = length(baseFileNames);
if numberOfImageFiles >= 1
% Go through all those files.
for f = 1 : numberOfImageFiles
existingFullFileName = fullfile(thisFolder, baseFileNames(f).name);
if isdir(existingFullFileName)
% Skip folders . and ..
continue;
end
% Get the last character of the folder. It should be a number from 1 to 4.
lastDigit = thisFolder(end);
% Create a new name for it.
newBaseFileName = sprintf('%s_images_%d.png', lastDigit, f-1);
newFullFileName = fullfile(thisFolder, newBaseFileName);
fprintf(' Renaming file %s to %s\n', existingFullFileName, newFullFileName);
% Do the actual renaming.
movefile(existingFullFileName, newFullFileName);
end
else
fprintf(' Folder %s has no files in it.\n', thisFolder);
end
end