Hello, I have a lot of .mat files in a folder and I want to rename it from ex : 65300106179 into something like Flight 10001 and so on. Below is my code
f = dir( fullfile( projectdir, '**', '*.mat') );
for kk = 1:numel(f)
fileFrom = f(kk).name;
fileTo = ['Flight',erase(f(kk).name,'1000%d.mat'),'.mat'];
movefile(fileFrom,fileTo);
end
The problem I have is the file name changed into Flight65300106179.mat. Any idea where I did wrong?

 Réponse acceptée

KSSV
KSSV le 3 Mar 2020
f = dir( fullfile( projectdir, '**', '*.mat') );
for kk = 1:numel(f)
fileFrom = f(kk).name;
fileTo = ['Flight','1000%d.mat','.mat'];
movefile(fileFrom,fileTo);
end

6 commentaires

Oktavian Jason
Oktavian Jason le 3 Mar 2020
the file name become Flight1000%d.mat, and the other files are gone....
Only 1 got changed into that
KSSV
KSSV le 3 Mar 2020
Modifié(e) : KSSV le 3 Mar 2020
f = dir( fullfile( projectdir, '**', '*.mat') );
for kk = 1:numel(f)
fileFrom = f(kk).name;
fileTo = ['Flight',num2str(kk),'.mat'];
movefile(fileFrom,fileTo);
end
Oktavian Jason
Oktavian Jason le 3 Mar 2020
Ok this works, but from Flight 1 and so on, I need to start from Flight 10001.
KSSV
KSSV le 3 Mar 2020
Modifié(e) : KSSV le 3 Mar 2020
f = dir( fullfile( projectdir, '**', '*.mat') );
for kk = 1:numel(f)
fileFrom = f(kk).name;
N = 10000+kk ;
fileTo = ['Flight',num2str(N),'.mat'];
movefile(fileFrom,fileTo);
end
Oktavian Jason
Oktavian Jason le 3 Mar 2020
thank you
Oktavian Jason
Oktavian Jason le 3 Mar 2020
Do you have any idea how I can rename on subfolder too?
Example : Folder A : Flight 10001-Flight 10010, Folder B : Flight 10011-Flight 10020

Connectez-vous pour commenter.

Plus de réponses (1)

Catégories

En savoir plus sur File Operations dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by