rename files using MATLAB

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 个评论

these are the subfolders -
and these are the image files inside each folder:
so I need to change name's of the subfolders like time002 to time001 (time003 to time002,..) and change the name of the files inside the subfolders (instead of slice001time002 to slice001time001)
Allmost there: If you want to change "time002" to "time001", should the existing "time001" become "time000"?
Then:
base = '\your\base\folder';
for k = 1:44 % Adjust manually
origName = sprintf('time%3d', k);
newName = sprintf('time%3d', k - 1);
origFolder = fullfile(base, origName);
newFolder = fullfile(base, newName);
movefile(origFolder, newFolder, 'f');
list = dir(fullfile(newName, '*.png'));
for kk = 1:numel(list)
origFileName = list(kk).name;
newFileName = strrep(origFileName, origName, newName);
movefile(fullfile(newFolder, origFileName), ...
fullfile(newFolder, newFileName));
end
end
As usual: Create a backup of the data before running the code.
Hi so I need to change 001 to 095 (the last file basically), but the code doesn't change the names of images inside the files (slice001time002 stays the same even if time002 changes to 001)
a ='C:\Users\ab7818\MATLAB\Images_work\time006';
A =dir( fullfile(a, '*.png') );
fileNames = { A.name };
for iFile = 1 : numel( A )
newName = fullfile(a, sprintf( 'slice%3.3dtime005.png', iFile ) );
movefile( fullfile(a, fileNames{ iFile }), newName );
end
I tried to manually change the names of pictures inside but this code changes the slice numbers in a haphazard manner
Before you actually do the renaming, I STRONGLY suggest you verify the filenames first with fprintf:
fprintf('Renaming\n %s\nto\n %s\n', fullfile(a, fileNames{ iFile }), newName );
% movefile( fullfile(a, fileNames{ iFile }), newName );
That way, if a mistkae is made you don't have to "undo" the naming.
@Image Analyst thank you so much! it shows that slices are being renamed in a random order :/
Again, explain "I need to change time002 to time001, time003 to time003, and so on."
So I read this as saying if the time is 00n (like 002) to decrement n so that the name is 00(n-1), (like 001), EXCEPT if the number is 3. If it's 3, do not rename it and leave it as 3.
Is that they way you want it?
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).
@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.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 File Operations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by