Copy/rename cell array and then rename all images again

조회 수: 1 (최근 30일)
Marcel Liphardt
Marcel Liphardt 2017년 8월 21일
댓글: Stephen23 2017년 8월 21일
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
Adam
Adam 2017년 8월 21일
Which aspect of it is causing the problem?
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에서 Construct and Work with Object Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by