MATLAB: How do i delete the contents of a folder

delete filesfile

could anyone tell me how its done please? I have a number of figures saved in the folder which i want to delete using Matlab. Thank you. the figures are named as figure1 , figure2 and so on. the number of figures i have in my folder changes everytime.

Best Answer

% Specify the folder where the files live.
myFolder = 'C:\whatever';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.fig'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now deleting %s\n', fullFileName);
delete(fullFileName);
end
Or use 'figure*.PNG' if you saved PNG images of your figures instead of .fig files.
Related Question