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!

 Respuesta aceptada

Fangjun Jiang
Fangjun Jiang el 26 de Jun. de 2020

0 votos

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

Más respuestas (2)

Nik Rocky
Nik Rocky el 26 de Jun. de 2020
Movida: Stephen23 el 14 de Nov. de 2026 a las 6:00
Thanks, but it is to complicated. I found easy way:
delete(fullfile(SubfolderPath,'*.png')) % or '*'
Thomas Carpenter
Thomas Carpenter el 13 de Nov. de 2026 a las 17:14
Editada: Thomas Carpenter el 13 de Nov. de 2026 a las 17:30
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

Categorías

Más información sobre Argument Definitions en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 26 de Jun. de 2020

Movida:

el 14 de Nov. de 2026 a las 6:00

Community Treasure Hunt

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

Start Hunting!

Translated by