Rename images using uigetfile and cell array
Mostra commenti meno recenti
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 Commenti
Michelangelo Ricciulli
il 14 Ago 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
il 14 Ago 2017
Risposta accettata
Più risposte (1)
Image Analyst
il 14 Ago 2017
Modificato: Image Analyst
il 15 Ago 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 Commenti
Marcel Liphardt
il 15 Ago 2017
Image Analyst
il 15 Ago 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
Categorie
Scopri di più su App Building in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!