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
채택된 답변
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
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 Center 및 File 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!