rename files using MATLAB

조회 수: 6 (최근 30일)
Ambika Bhardawaj
Ambika Bhardawaj 2022년 2월 4일
댓글: Jan 2022년 2월 7일
I need to rename my subfolders and files in those subfolders in an ascending order (subfolders are named time002, time003,... & files inside are named slice001time002, slice002time002,...). I need to change time002 to time001, time003 to time003, and so on. I have the base of the code but I am not sure how to go about changing it.
% Specify the folder where the files live.
myFolder = 'C:\Users\Ambika\Desktop\Images';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir();
if myFolder == 0
return;
end
end
% Get a list of all files in the folder, and its subfolders, with the desired file name pattern.
filePattern = fullfile(myFolder, '/time%3.3d/slice%3.3dtime%3.3d.png');
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, '/time%3.3d/slice%3.3dtime%3.3d.png', fullFileName);
imageArray = imread(fullFileName);
imshow(imageArray);
drawnow;
end

답변 (1개)

Jan
Jan 2022년 2월 4일
편집: Jan 2022년 2월 4일
This cannot work:
filePattern = fullfile(myFolder, '/time%3.3d/slice%3.3dtime%3.3d.png');
theFiles = dir(filePattern);
The file pattern contains a pattern for the creation of strings, but this does not find matching file names.
this is failing also:
fprintf(1, '/time%3.3d/slice%3.3dtime%3.3d.png', fullFileName)
The pattern expects 3 nuimerical values, but fullFileName is a char vector. A valid command would be:
fprintf(1, '%s', fullFileName)
How are the original files stored? The shown code does not perform any renaming and does not work at all. What does this mean: "change time003 to time003"?
What are the original names and what have to be changed to what?
  댓글 수: 9
Ambika Bhardawaj
Ambika Bhardawaj 2022년 2월 6일
편집: Ambika Bhardawaj 2022년 2월 6일
no (it was a typo) so the subfolders have to decrement but those subfolders have 79 .png files inside them named slice001time00n, so the code changes the subfolders but the png flies remain slice00ntime00n instead of slice00ntime00(n-1).
Jan
Jan 2022년 2월 7일
@Ambika Bhardawaj: "Hi so I need to change 001 to 095 (the last file basically)" - adding such details after a longer discussion is a shot in your knee. If you explain this directly, the users of the forum do not waste their and your time with posting solutions, which do not match your problem.
I cannot run my suggested code, because I do not have your data. So it is your turn to use the debugger to check, what's going on. Set a breakpoint in the line:
newFileName = strrep(origFileName, origName, newName);
and find out, if it creates the names as expected or if there is a problem. Use my code as point to start from to create, what you exactly need.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by