Loop for renamed files from a folder

조회 수: 6 (최근 30일)
Antony MARCHIS 2021년 7월 13일
댓글: ANKUR KUMAR 2021년 7월 15일
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.

채택된 답변

ANKUR KUMAR 2021년 7월 13일
편집: ANKUR KUMAR 2021년 7월 13일
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', ...
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')), ...
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
Antony MARCHIS 2021년 7월 15일
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 2021년 7월 15일
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

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

추가 답변 (0개)


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




Community Treasure Hunt

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

Start Hunting!

Translated by