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
Related
Currently, I have data in two separate folders (say folder A and folder B).
I have a for loop code which analyses the relevant variables for each participant. The beginning portion of my for loop runs through the variables found in data contained in folder A; however, I would like to include variables from the data in folder B at the end of my for loop.
How would I go about doing this?
Thanks for your help in advance!
I've tried adding a line to change the directory once the loop reaches the portion of the loop for extracting the variables from folder B. But this causes a
Error using load' 'X file is not found in the current folder.
Now I have placed both directories at the beginning of my script and tried loading them both before the loop starts.
cd('folderAPath')
addpath('folderBPath')
filesA = dir(filenameA)
filesB = dir(filenameB)
When trying to use the load() function for files A and B, the files from folder B do not load into my workspace.
You can change the current working folder before calling the dir (...) command to point to the correct folder.
cd('folderAPath')
filesA = dir(filenameA)
cd('folderBPath')
filesB = dir(filenameB)
My guess is that Matlab cannot locate the files in folder B.
Say your directory tree is something like this:
--root_folder
--folderA
--folderB
If you change directory (cd) to folderA, folderB is not visible if you don't provide the entire path (from root folder).
One of these approaches will solve your problem:
Provide the entire path (from root folder) to the load function
cd to folder B and load your data
Sounds like you're trying to load the files in folderB when folderB isn't on your filepath.
for example if the path to the files are as follows:
C:/Users/.../.../MATLAB/folderA
C:/Users/.../.../MATLAB/folderB
have the following above and outside of your loop:
addpath('C:/Users/.../.../MATLAB/folderA');
addpath('C:/Users/.../.../MATLAB/folderB');
Then just do something like the following pseudocode:
numFilesA = number of files in folder A
numFilesB = number of files in folder B
totalFiles = numFilesA + numFilesB
for i in totalFiles
if i <= numFilesA
load folderA/file(i)
else % we've read all the files in folderA
load folderB/file(i-numFilesA) % because we want to load from index 1 of the new folder
% and do whatever other stuff you want to do in here
end
end
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{:})
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
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.
I have a problem loading multiple images at the same time using matlab. Could anybody me?
How about writing a small program?
'uigetdir' [http://www.mathworks.com/help/techdoc/ref/uigetdir.html] to let user to select the directory where image files are.
'dir' and determine the names of the files in that directory.
'listdlg' to create a list of files on a GUI, with 'SelectionMode' as 'multiple'
check the file extension (you can do this before #3 also to show only image files in the list.)
count (N) how many image files the user wants to load and plot ('length' of the selected filename string).
loop for N times and go through the list of filenames, and open each one with the appropriate loader function (by determining the file extension of each file before loading)
as you load the data from the files, you can plot them however you like either in a single figure or multiple.
Best,
Y.T.