Copy/rename cell array and then rename all images again

1 次查看(过去 30 天)
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 个评论
Marcel Liphardt
Marcel Liphardt 2017-8-21
What do you mean by causing the problem?
I just would like to get some help to achieve all this.

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2017-8-21
编辑:Stephen23 2017-8-21
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 个评论
Marcel Liphardt
Marcel Liphardt 2017-8-21
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 2017-8-21
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by