Loop for renamed files from a folder

6 views (last 30 days)
Hello everyone,
I'm a beginner on matlab, today I tried to rename my files names without succes.
Below you can find my problem :
I have files named like this : I would like rename like this :
S01_0_VJ_01 S01_0_VJ_01
S01_0_VJ_02 S01_0_VJ_02
S01_0_VJ_03 S01_0_VJ_03
S01_0_SP_04 S01_0_SP_01
S01_0_SP_05 S01_0_SP_02
S01_0_SP_06 S01_0_SP_03
S01_4_VJ_07 S01_4_VJ_01
S01_4_VJ_08 S01_4_VJ_02
S01_4_VJ_09 S01_4_VJ_03
S01_4_SP_10 S01_4_SP_01
S01_4_SP_11 S01_4_SP_02
S01_4_SP_12 S01_4_SP_03
S01_8_VJ_13 S01_8_VJ_01
S01_8_VJ_14 S01_8_VJ_02
S01_8_VJ_15 S01_8_VJ_03
S01_8_SP_16 S01_8_SP_01
S01_8_SP_17 S01_8_SP_02
S01_8_SP_18 S01_8_SP_03
each condition start by 01 and end by 03.
The first things that I do is
cd 'C:\Users\..............'
d = dir('*.c3d');
It's c3d files.
Thank you in advance for your help.

Accepted Answer

ANKUR KUMAR
ANKUR KUMAR on 13 Jul 2021
Edited: ANKUR KUMAR on 13 Jul 2021
You can find the unique prefixes in the filename, and then use it to generate the string of numbers to rename the file. Here is an example to generate the filenames as per your need.
filename={'S01_0_VJ_01', 'S01_0_VJ_02', 'S01_0_VJ_03', 'S01_0_SP_04', 'S01_0_SP_05', ...
'S01_0_SP_06','S01_4_VJ_07','S01_4_VJ_08','S01_4_VJ_09','S01_4_SP_10','S01_4_SP_11',...
'S01_4_SP_12','S01_8_VJ_13','S01_8_VJ_14','S01_8_VJ_15','S01_8_SP_16','S01_8_SP_17','S01_8_SP_18'};
unique_prefix=unique(cellfun(@(x) x(1:8), filename,'uni', 0),'stable')
unique_prefix = 1×6 cell array
{'S01_0_VJ'} {'S01_0_SP'} {'S01_4_VJ'} {'S01_4_SP'} {'S01_8_VJ'} {'S01_8_SP'}
for unique_pre_index=1:length(unique_prefix)
new_filename{unique_pre_index}=arrayfun(@(x) strcat(unique_prefix{unique_pre_index},{'_'},num2str([x],'%02d')), ...
1:length(find(contains(filename,unique_prefix{unique_pre_index}))));
end
new_filenames=cat(2,new_filename{:});
old_new_filenames=[filename; new_filenames]'
old_new_filenames = 18×2 cell array
{'S01_0_VJ_01'} {'S01_0_VJ_01'} {'S01_0_VJ_02'} {'S01_0_VJ_02'} {'S01_0_VJ_03'} {'S01_0_VJ_03'} {'S01_0_SP_04'} {'S01_0_SP_01'} {'S01_0_SP_05'} {'S01_0_SP_02'} {'S01_0_SP_06'} {'S01_0_SP_03'} {'S01_4_VJ_07'} {'S01_4_VJ_01'} {'S01_4_VJ_08'} {'S01_4_VJ_02'} {'S01_4_VJ_09'} {'S01_4_VJ_03'} {'S01_4_SP_10'} {'S01_4_SP_01'} {'S01_4_SP_11'} {'S01_4_SP_02'} {'S01_4_SP_12'} {'S01_4_SP_03'} {'S01_8_VJ_13'} {'S01_8_VJ_01'} {'S01_8_VJ_14'} {'S01_8_VJ_02'} {'S01_8_VJ_15'} {'S01_8_VJ_03'} {'S01_8_SP_16'} {'S01_8_SP_01'} {'S01_8_SP_17'} {'S01_8_SP_02'} {'S01_8_SP_18'} {'S01_8_SP_03'}
In order to read all the filenames from a folder, you can use dir command to read all the files, and use this output to extract the filenames. Here is an example for this (You can uncomment the below two lines when running in your terminal):
% F=dir('S01*')
% filenames={F().name}
  2 Comments
Antony MARCHIS
Antony MARCHIS on 15 Jul 2021
Thank you so much for your help, It works.
Now I would like to replace old_new_filenames by new_filenames directly in the folder. Is it possible ?
ANKUR KUMAR
ANKUR KUMAR on 15 Jul 2021
You can do it either using movefile command or system command. Refer this answer for detailed steps. https://www.mathworks.com/matlabcentral/answers/571819-renaming-files-using-matlab

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!