필터 지우기
필터 지우기

need a script to rename and resort TIF files

조회 수: 4 (최근 30일)
Mohamed
Mohamed 2023년 10월 17일
댓글: Mohamed 2023년 10월 18일
I have a group of TIF files with serial such as:
Im_000000
Im_000001
Im_000002
Im_000003
Im_000004
and I need to rename and resort them to be as follow
Im_000000.B
Im_000001.A
Im_000001.B
Im_000002.A
Im_000002.B
i use the below script, but it can only to rename the file with A and B without sorting
clear all; clc;
INDIV = 'C:\Users\Desktop\New folder\';
filenames_INDIV = dir(fullfile(INDIV, '*.tif'));
for filenum= 1 : numel(filenames_INDIV);
if rem(filenum,2) == 0
[~, basename] = fileparts(filenames_INDIV(filenum).name);
newname = sprintf('%s_%04d-imgA.tif', basename, filenum(:));
movefile(fullfile(INDIV, filenames_INDIV(filenum).name), fullfile(INDIV, newname));
end
if rem(filenum,2) == 1
[~, basename] = fileparts(filenames_INDIV(filenum).name);
newname = sprintf('%s_%04d-imgB.tif', basename, filenum(end,:));
movefile(fullfile(INDIV, filenames_INDIV(filenum).name), fullfile(INDIV, newname));
end
end

채택된 답변

Jan
Jan 2023년 10월 18일
편집: Jan 2023년 10월 18일
Folder = 'C:\Users\Desktop\New folder\';
Files = dir(fullfile(Folder, '*.tif'));
Keys = {'A', 'B'};
[~, name] = fileparts(Files{1});
name = name(1:find(name == '_', 1, 'last')); % Correct assumption?
for iFile = 1:numel(Files)
newname = sprintf('%s_%06d%s.tif', ...
name, ... % 'Im_07291355_'
ceil((iFile - 1) / 2), ... % '000000', '000001', '000001', ...
Keys{rem(iFile, 2) + 1} ); % 'B' or 'A'
disp(newname) % Try it at forst before uncommenting:
% movefile(fullfile(Folder, Files(iFile).name), fullfile(Folder, newname));
end
  댓글 수: 2
Mohamed
Mohamed 2023년 10월 18일
I tried the script, but this error apears to me:
>> [~, name] = fileparts(Files{1});
Brace indexing is not supported for variables of this type.
Mohamed
Mohamed 2023년 10월 18일
Many thanks, it works!!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

태그

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by