Loop for renamed files from a folder
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Antony MARCHIS
el 13 de Jul. de 2021
Comentada: ANKUR KUMAR
el 15 de Jul. de 2021
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.
0 comentarios
Respuesta aceptada
ANKUR KUMAR
el 13 de Jul. de 2021
Editada: ANKUR KUMAR
el 13 de Jul. de 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')
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]'
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 comentarios
ANKUR KUMAR
el 15 de Jul. de 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
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!