I have six folders like this >> Images and each folder contains some images. I know how to read images in matlab BUT my question is how I can traverse through these folders and read images in abc.m file (this file is shown in this image)
3 Answers
So basically you want to read images in different folders without putting all of the images into one folder and using imread()
? Because you could just copy all of the images (and name them in a way that lets you know which folder they came from) into a your MATLAB working directory and then load them that way.
Use the cd
command to change directories (like in *nix) and then load/read the images as you traverse through each folder. You might need absolute path names.

- 889
- 1
- 7
- 16
-
thanks what about going back to previous (parent) directory ? like in DOS its `cd..` – Muaz Usmani May 27 '12 at 20:00
-
`cd ..` The `.` refers to the current directory, `..` refers to its parent. – Adam27X May 27 '12 at 20:02
-
means `cd '..';` will move me to its parent directory ? – Muaz Usmani May 27 '12 at 20:06
-
No quotes, just: `cd ..` Also, if you know the absolute path of the parent directory you can use that as well. – Adam27X May 27 '12 at 20:08
-
the quotes _are needed_ when you use it as a function! see http://www.mathworks.nl/help/techdoc/matlab_prog/f10-60415.html#f10-60461 – Gunther Struyf May 27 '12 at 21:39
The easiest way is certainly a right clic on the forlder in matlab and "Add to Path" >> "Selected Folders and Subfolders"
Then you can just get images with imread
without specifying the path.

- 15,075
- 9
- 59
- 98
if you know the path to the image containing directory, you can use dir on it to list all the files (and directories) in it. Filter the files with the image extension you want and voila, you have an array with all the images in the directory you specified:
dirname = 'images';
ext = '.jpg';
sDir= dir( fullfile(dirname ,['*' ext]) );;
sDir([sDir.isdir])=[]; % remove directories
% following is obsolete because wildcarded dir ^^
b=arrayfun(@(x) strcmpi(x.name(end-length(ext)+1:end),ext),sDir); % filter on extension
sFiles = sDir(b);
You probably want to prefix the name of each file with the directory before using:
sFileName(ii) = fullfile(dirname, sFiles(ii));
You can process this resulting files as you want. Loading all the files for example:
for ii=1:numel(sFiles)
data{i}=imread(sFiles(ii).name)
end
If you also want to recurse the subdirectories, I suggest you take a look at:
How to get all files under a specific directory in MATLAB?
or other solutions on the FEX:
http://www.mathworks.com/matlabcentral/fileexchange/15505-recursive-dir
EDIT: added Amro's suggestion of wildcarding the dir call

- 1
- 1

- 11,158
- 2
- 34
- 58
-
1instead of manually filtering the files, you could specify something like: `dir('folder\*.ext')`. Here is an example: http://stackoverflow.com/a/7293443/97160 – Amro May 27 '12 at 21:58
-
@Amro: I was thinking that such wildcard didn't work.. I guess I'm confusing it with something else :p thx for the tip! – Gunther Struyf May 27 '12 at 22:41