A.A.
A.A.

Reputation: 1047

delete files with numbered names in matlab directory

I'm new to matlab and I've wrote a code that implements the gamma function for image processing. I generate around 300 photos named '001.jpg' to '300.jpg' and then use ffmpeg to make a video. In the end, I only need the video result and need a command to delete all the photos generated in the directory! is there a way to do that?

Upvotes: 0

Views: 512

Answers (2)

Omer
Omer

Reputation: 98

Adding to Suever's answer (not allowed to comment yet):
Assuming you already know the names of the images you're creating, you could save your script a 'trip' to the folder and back by creating the filenames list yourself thus:

for i=1:numOfImages
filenames(i)={strcat(num2str(i),'.jpg')};
end

delete(filenames{:})

Upvotes: 0

Suever
Suever

Reputation: 65460

If you want to remove all .jpg files in the current directory you can use the delete command with a wildcard (*)

delete('*.jpg')

If the files live in a folder other than the current directory, you can specify the directory in this way.

folder = '/path/to/my/files';
delete(fullfile(folder, '*.jpg'))

If you want to limit it to just files that have number filenames, you could do something like the following

files = dir('*.jpg');
filenames = regexp({files.name}, '^[0-9]+\.jpg$', 'match', 'once');
filenames = cellstr(cat(1, filenames{:}));

delete(filenames{:})

Upvotes: 1

Related Questions