rename files using MATLAB
4 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
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 个评论
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 Center 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

