Loop for renamed files from a folder

4 ビュー (過去 30 日間)
Antony MARCHIS
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
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', ...
'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 件のコメント
Antony MARCHIS
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
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 Center および File ExchangeProgramming についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by