Copy/rename cell array and then rename all images again

1 visualización (últimos 30 días)
Marcel Liphardt
Marcel Liphardt el 21 de Ag. de 2017
Comentada: Stephen23 el 21 de Ag. de 2017
Hello,
currently my code to rename images in general looks like this:
function left(~)
[filenames, pathname] = uigetfile({'*.jpg'}, 'MultiSelect', 'on', 'Please select the left images');
fullFilenames1 = cellfun( @(x) fullfile( pathname, x ), filenames, 'UniformOutput', false );
for iFile = 1:numel(fullFilenames1)
newName = sprintf('left%03d.jpg',iFile);
movefile(fullFilenames1{iFile},newName);
end
All my images are called e.g.: left01 078, left01 079, left01 80.......
Now that we have two cameras, we also get these right images:
right01 078, right01 079, right01 80......
This means that the right images have to be renamed automatically, so that it would be easier for the user.
So just before the loop starts, my 'filenames' cell array needs to be duplicated, then the names inside need to be renamed from left01... to right01 but still containing the numbers at the end mentioned above.
Then the user has to choose: folder = uigetdir(); the directory where the right images are stored, so that it can add the path to the fullfile?????
And last but not least, another loop then renames the right images just like the loop before:
for iFile = 1:numel(fullFilenames2)
newName = sprintf('right%03d.jpg',iFile);
movefile(fullFilenames2{iFile},newName);
end
I tried to search for these things but I couldn't find any suitable answer as of yet.
  2 comentarios
Adam
Adam el 21 de Ag. de 2017
Which aspect of it is causing the problem?
Marcel Liphardt
Marcel Liphardt el 21 de Ag. de 2017
What do you mean by causing the problem?
I just would like to get some help to achieve all this.

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 21 de Ag. de 2017
Editada: Stephen23 el 21 de Ag. de 2017
Use regexprep or strrep, e.g.:
[filenames, pathname] = uigetfile({'*.jpg'}, 'MultiSelect', 'on', 'Please select the left images');
LNames = cellfun( @(x) fullfile( pathname, x ), filenames, 'UniformOutput', false );
RNames = strrep(LNames,'left','right');
It might be even simpler to use dir to get all of the filenames, then you do not force the user to select them all:
D = dir(fullfile(pathname,'left*.jpg'));
LNames = {D.name};
  5 comentarios
Marcel Liphardt
Marcel Liphardt el 21 de Ag. de 2017
Yeah ok, sorry that I didn't tell you.
So all my left images are stored in their own folder.
And all my right images are stored in their oe´wn folder as well.
Stephen23
Stephen23 el 21 de Ag. de 2017
Then you could do something like this:
txt = 'Please select the left images';
[Lnames, Lpath] = uigetfile({'*.jpg'}, 'MultiSelect','on', txt);
Rnames = strrep(Lnames,'left','right'); % or regexprep for more control
Rpath = uigetdir();
for k = 1:numel(Lnames)
movefile(fullfile(Lpath,Lnames{k}), sprintf('left%03d.jpg',k));
movefile(fullfile(RPath,Rnames{k}), sprintf('right%03d.jpg',k)),
end

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Construct and Work with Object Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by