Batch renaming in matlab

3 次查看(过去 30 天)
Jack Turner
Jack Turner 2015-6-4
Hello all,
I have a selection of images of a, optical calibration target that need to be renamed in order to be effectively processed. The file tree that appears like so:
2, 4, 6, 8, 10 and in each of these numbered folders it contains another set of folders as so:
......+3um ......+5um ......+10um ......+15um
Inside each subfolder is a file named B00001.IM7.
What I am trying to do is take each B00001.IM7 file and move it up one level in the directory, then let it take the name of it's parent folder to eliminate the bottom level of the tree.
The naming of the level above these files was +3um and -8um etc,
These need to be renamed as +0.003x and -0.008x so that there is a +/- at the start, then the number in microns is converted into millimeters and all with an x on the end.
I hope this query makes sense and any help would be greatly appreciated,
Kind Regards, Jack

回答(1 个)

Cindy Solomon
Cindy Solomon 2015-6-5
Hi Jack,
In order to move or rename files programmatically, I would recommend looking at the "movefile" function. I'm not sure if I understand your entire question, but assuming in your main file tree, you have folders titled 2, 4, 6, 8, and 10, you can programmatically move into these folders by "cd"ing into them, getting a list of the folder names inside of them (assuming the only folders are +3um, +5um, etc), moving those files up one level, and then renaming them after moving. If you wanted to add "0s" before the number, you could process the strings (ex: taking the first character, concatenating zeros in the middle, and then adding the last characters after that), but below is a small example of how to do everything but that:
mainFileTree = {'2','4','6','8','10'}; % Make sure these are the full path to the folders
for i = 1:5
% For each of the main folders (2, 4, 6, 8, 10)
cd(mainFileTree{i}) % Change to appropriate directory
dirData = dir;
folderNames = {dirData.name};
for j = 1:4
currentFolderName = folderNames{j}; % Figure out which folder it is in
cd(folderNames{j})
movefile('B0001.m','../') % Move up one level
% Rename file
movefile('B0001.m', [currentFolderName 'x.m'])
end
Is there a particular reason why your files need to have those specific names in that particular structure to be processed? If you are doing the processing in MATLAB, you should be able to navigate through the appropriate directories no matter what the name is.
Hope this helps!

类别

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