Rename images using uigetfile and cell array
Afficher commentaires plus anciens
Greetings,
so Im using the following function to rename my images that I would like to open via the uigetfile and then try to rename them with the movefile method. This code only works when the images are in the matlab path.
function output(~)
dirData = dir('*.jpg');
fileNames = {dirData.name};
for iFile = 1:numel(fileNames)
newName = sprintf('left%03d.jpg',iFile);
movefile(fileNames{iFile},newName);
end
But now if I try to input this:
dirData = uigetfile({'*.jpg'},'MultiSelect', 'on', 'Please selecet the images');
instead of:
dirData = dir('*.jpg');
then it just gives me the error message: Struct contents reference from a non-struct array object. Error in output (line 4) fileNames = {dirData.name};
3 commentaires
Michelangelo Ricciulli
le 14 Août 2017
That's because the uigetfile function already returns the file names in a cell array. The output differs from the dir function.
I think it's sufficient to skip that line and write
fileNames=uigetfile({'*.jpg'},'MultiSelect', 'on', 'Please selecet the images');
Marcel Liphardt
le 14 Août 2017
Réponse acceptée
Plus de réponses (1)
Image Analyst
le 14 Août 2017
Modifié(e) : Image Analyst
le 15 Août 2017
Try this:
folder = pwd; % Or use getdir().
filePattern = fullfile(folder, '*.jpg'); % Don't use JPG images for image analysis.
dirData = dir(filePattern);
for k = 1 : length(dirData)
thisFileName = fullfile(folder, dirData(k).name);
newName = sprintf('left%03d.jpg',k);
newFileName = fullfile(folder, newName);
fprintf('Renaming %s to %s\n', dirData(k).name, newName);
movefile(thisFileName, newName);
end
2 commentaires
Marcel Liphardt
le 15 Août 2017
Image Analyst
le 15 Août 2017
You can use this code if you want the user to specify the files:
folder = pwd; % Starting folder.
filePattern = fullfile(folder, '*.jpg'); % Don't use JPG images for image analysis.
[filenames, folder] = uigetfile(filePattern, 'MultiSelect', 'on', 'Please select the images');
for k = 1 : length(filenames)
thisFileName = fullfile(folder, filenames{k});
newName = sprintf('left%03d.jpg',k);
newFileName = fullfile(folder, newName);
fprintf('Renaming %s to %s\n', filenames{k}, newName);
movefile(thisFileName, newFileName);
end
Catégories
En savoir plus sur App Building dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!