rename files using data in cell array

1 回表示 (過去 30 日間)
pallavi sri
pallavi sri 2022 年 3 月 18 日
編集済み: Voss 2022 年 3 月 18 日
hi, I have a list of 135 .mat files with complete file path. I have to take subjectid from the file path ( for eg. /project/brain/12345/matrix_456.mat) I want to copy these files into a new folder and rename them with brain_id_filename. So for this case it will be brain_12345_matrix_456.mat. I was able to copy these files into a new folder using copyfile but i am unable to rename them. thanks in advance.

回答 (1 件)

Voss
Voss 2022 年 3 月 18 日
編集済み: Voss 2022 年 3 月 18 日
You can rename files using copyfile() or movefile().
In either case the destination argument would be the new name of the file (not a folder), and it's best to use full absolute paths, e.g.:
copyfile('/home/project/brain/12345/matrix_456.mat','/home/project/brain_12345_matrix_456.mat');
  2 件のコメント
pallavi sri
pallavi sri 2022 年 3 月 18 日
Thanks but i have 135 . mat files, so i was running this in loop, with destination as the destination directory
Voss
Voss 2022 年 3 月 18 日
編集済み: Voss 2022 年 3 月 18 日
If you want to rename the files, destination will have to be the new file name. It will be different each time through the loop.
You can set up a cell array containing the new file names, e.g.:
old_files = { ...
'/project/brain/12345/matrix_456.mat'; ...
'/project/brain/12345/matrix_789.mat'; ...
'/project/brain/99999/matrix_000.mat'}; % you say you have this already
new_files = strrep(old_files,filesep(),'_') % something like this, maybe not exactly
new_files = 3×1 cell array
{'_project_brain_12345_matrix_456.mat'} {'_project_brain_12345_matrix_789.mat'} {'_project_brain_99999_matrix_000.mat'}
for ii = 1:numel(new_files)
copyfile(old_files{ii},new_files{ii});
end

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Import and Analysis についてさらに検索

製品


リリース

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by