delete files with numbered names in matlab directory - matlab

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?

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{:})

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{:})

Related

Make a copy of the files in a directory but save them as a different file type

Is it possible to list all of the files in a directory of a certain file type, and then save them in the same directory but as a separate file type using MatLab?
In my case, I have 144 files saved as in a .fig format, but I would like to copy them as a .tif format so that I don't have to go and change each file manually.
I know I can list all the files in my directory using the dir function and I guess I could simply run for loop with i=1:length(dir) but I don't know how to isolate the files of a specific file type. I don't see filetype as a field name on the mathworks website for dir.
Thanks for any suggestions.
To list only files of type .fig:
files = dir('*.fig');
You can then loop over the names:
for k = 1:numel(files)
filename = files(k).name;
% do something with filename
end

MatLab - accessing subfolders of folders saved as variables

I have the following code which can create a directory within a selected folder:
photos_dir = 'C:\Users\Bob\Photos';
mkdir(photos_dir,'Christmas 2015')
I'd like to then be able to save an image to this folder, I think using something like:
imwrite(img,Christmas 2015,'jpg')
However, this does not select the "Christmas 2015" folder which is in the "\Photos" directory. How can I make the image be written to this location?
First of all, you're going to have a syntax error since Christmas 2015 should at least be a string. But other than that, if you want to save a file in a particular location (other than the current working directory), you need to provide the full path to the file location.
To do this, you need to use fullfile to combine all of your directory and file names together into a full file path.
image_name = fullfile(photos_dir, 'Christmas 2015', 'yourphoto.jpg');
imwrite(img, image_name, 'jpg')

How to read trc files from different directory in matlab

I have a program that reads data from .trc files. But as of now, it can only read the .trc files that are in the current directory i.e the MATLAB folder. If I want it to read a file, I have to copy the file in MATLAB folder. Is there a way I can move to different directories and choose another .trc file? I have tried using dir, uigetdir etc. but nothing seems to work!
It's pretty straightforward to filter results with the dir command with a file extension if you specify the folder to search with a wildcard character in place of the file name. You can then loop over the generated list of file names in the output structure (as shown) or whatever you need to do with those files.
folderName = 'C:\Path\To\Target\Folder\';
fileList = dir(strcat(folderName, '*.trc'));
for k = 1:length(fileList)
fileHere = fullfile(folderName, fileList(k).name);
% Do what you need with the files in here
end

concatenating file path to changing folders

I am somewhat new to MATLAB and am trying to set up a changing file path in a loop to go into a series of folders and grab image files from each folder. I'm not sure if the problem is with the concatenated parts of the path itself, or with the wildcard search I am using.
I've used similar changing file paths before that have worked, but this one is giving me a "Index exceeds matrix dimensions" error. I thought it was the '*' element that was problematic (similar concatenated paths have worked for me, but only when I specify a file extension or part of a file name), but I am trying to grab DICOM files that do not have any extension, which might make it difficult.
The line within the for loop is as follows:
inputs{1, crun} = cellstr(spm_select('FPList'[allinput,'T1Rawunzip',filesep,OrderForDicoms3{crun,1}],'*'));
I've tried different ways of specifying this--using spm_select, not using spm_select, using commas instead of filesep or vice versa, but nothing has worked.
Any advice would be very much appreciated.
(for reference:
crun is the counter the moves the loop forward, 'allinput' is a previously-specified path, OrderForDicoms3 is a .mat file with a list of folder names that are being individually concatenated to the path each time the loop runs)
Thanks!
-Victoria
I can tell you the most general approach of grabbing files from a folder. If you specify the input folder through uigetdir, then all the files can be grabbed using dir command:
folder = uigetdir;
files = dir(folder);
for i =1:length(files)
if(~files.isdir())
filename = fullfile(folder, files(i).name);
% ... read in the data %
end
end
You can always do it for multiple levels.

Matlab multiple image loading / processing

I'm trying to process 77 images on a single matlab script. I have to load these images and use several processing functions on each. The image names are not sequential. How can I do that without explicitly writing all?
Thanks in advance.
you can use dir to get a list of all the files and folders in the current folder you are in, for example
s=dir(fullfile(matlabroot, 'toolbox/matlab/audiovideo'))
returns the contents of the matlab/audiovideo folder, where s.name will contain the names of the files in that folder.
Another example:
s=dir('*.mat')
will return to s.name all the file names in the current folder that are of type .mat
Now you can load these files in a loop:
for n=1:numel(s)
load(s(n).name);
.... % do whatever
end