How to name files in a folder based on parent folder?

1 回表示 (過去 30 日間)
Hamed Majidiyan
Hamed Majidiyan 2022 年 5 月 9 日
コメント済み: Hamed Majidiyan 2022 年 5 月 10 日
Hello all,
I'm newbie to Matlab and I got a question regarding naming files in folders. I have 6 folders let's say simulation_data1 to 6, and there are 17 pictures in each folders which are named block_1 to 17 now in all 6 folders. I was wondering how can I name each batch of pictures (17 figs) in every folder based on the name of parent folder? For example, all figs in folder simulation_data1 become simulation_data1_1 to 17.
any help would be appreciated.
regards

採用された回答

Chunru
Chunru 2022 年 5 月 9 日
for i=1:6 % 6 folders
foldername = sprintf('simulation_data%d', i)
mkdir(foldername)
cd(foldername)
for j=1:2 % 2 files for example
filename = sprintf('%s_%d', foldername, j);
fprintf(' Filename: %s\n', filename)
save(filename, 'filename'); % create a file
end
cd( '..');
end
foldername = 'simulation_data1'
Filename: simulation_data1_1 Filename: simulation_data1_2
foldername = 'simulation_data2'
Filename: simulation_data2_1 Filename: simulation_data2_2
foldername = 'simulation_data3'
Filename: simulation_data3_1 Filename: simulation_data3_2
foldername = 'simulation_data4'
Filename: simulation_data4_1 Filename: simulation_data4_2
foldername = 'simulation_data5'
Filename: simulation_data5_1 Filename: simulation_data5_2
foldername = 'simulation_data6'
Filename: simulation_data6_1 Filename: simulation_data6_2
dir
. .. simulation_data1 simulation_data2 simulation_data3 simulation_data4 simulation_data5 simulation_data6
  5 件のコメント
Chunru
Chunru 2022 年 5 月 10 日
Just remove it. It is used to generate data (as I don't have your data).
Hamed Majidiyan
Hamed Majidiyan 2022 年 5 月 10 日
Thanks a lot. I fixed it.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by