Hello,
I have a folder:
Testfolder
Inside are:
1.mat
1.txt
1.wav
SubTestfolder
Inside SubTestfolder are:
2.mat
2.txt
2.wav
I do
Del_path = fullfile(TestFolderPath,SubTestFoldername);
delete(Del_path,'*.mat')
I was waiting that 2.mat should be deleted, but it will be 1.mat deleted, not 2.mat. Why? How can I delete files/all files in subfolder?
Thank you!

 採用された回答

Fangjun Jiang
Fangjun Jiang 2020 年 6 月 26 日

0 投票

use dir('**/*.mat') to find files in subfolders in one shot and then use a for-loop to delete the files.

その他の回答 (2 件)

Nik Rocky
Nik Rocky 2020 年 6 月 26 日
移動済み: Stephen23 2026 年 11 月 14 日 6:00

1 投票

Thanks, but it is to complicated. I found easy way:
delete(fullfile(SubfolderPath,'*.png')) % or '*'
Thomas Carpenter
Thomas Carpenter 2026 年 11 月 13 日 17:14
編集済み: Thomas Carpenter 2026 年 11 月 13 日 17:30

0 投票

A clean solution to delete all matching files recursively in subfolders matching a wildcard:
files = dir(fullfile(searchDir,'**/*.mat'))
if ~isempty(files)
files = fullfile({files.folder}, {files.name});
delete(files{:})
end

カテゴリ

ヘルプ センター および File Exchange で Argument Definitions についてさらに検索

タグ

質問済み:

2020 年 6 月 26 日

移動済み:

2026 年 11 月 14 日 6:00

Community Treasure Hunt

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

Start Hunting!

Translated by