Unknown File/Folder in directory

5 views (last 30 days)
HI, I'm one of the enw users of Matlab.
I'm Having some problem in folder and sub folder reading......
Look at this Code....
Name = '.\train20X20\'
Main_File = dir(fullfile(Name));
Normal access to a folder, but this folder has 34 sub folder in it but when I compile this line, on the (Workspace) Tab the variable Main_File shows that there are 36 folders in it.
What does this mean, I dont Understand.
In the Main folder there are 34 subfolders having 10 images each. what is wrong.
after that when i try to make a loop to access the subfolder using there names it shows an error message.
I tried bothg with Curly Brackets and Small Brackets but the same message appeared.
Few Monthes Earliar i worked on something similar, the function worked perfectly but this time it's not working i dont know what wrong.
  1 Comment
Stephen23
Stephen23 on 24 Jan 2021
Edited: Stephen23 on 24 Jan 2021
The simple way to ignore the '.' and '..' folders:
S = dir(Folder_Name);
S = S(~ismember({d.name},{'.','..'}));

Sign in to comment.

Accepted Answer

per isakson
per isakson on 23 Jan 2021
Edited: per isakson on 24 Jan 2021
"What does this mean, I dont Understand." I assume that you refer to 34 and 36, respectively.
Main_File is a struct array with one element for each file/folder in the folder, "\train20X20\". And in addition two elements (on Windows at least), which represent the folder itself and its parent folder.
>> Main_File(1:2).name
ans =
'.'
ans =
'..'
"using there names it shows an error message" Main_File is not a name, it's a struct. Main_File.name and Main_File.folder are names.
See: Debug a MATLAB Program and then step through the code line by line and inspect the values of the variables.
A different approach (added later)
"Main folder there are 34 subfolders having 10 images each" If the main folder doesn't contain anything else and you don't need to distinguish between the images of the different subfolders.
%%
MainFolder = 'something';
sad = dir( fullfile( MainFolder, '**', '*.jpg' ) );
for jj = 1 : numel(sad)
ffs = fullfile( sad(jj).folder, sad(jj).name );
image_matrix = imread( ffs );
% your code
end
  3 Comments
Image Analyst
Image Analyst on 23 Jan 2021
if strcmp(image_path, '.') || strcmp(image_path, '..')
continue; % Skip to bottom of loop.
end
Mir AbdulRehman
Mir AbdulRehman on 24 Jan 2021
Thanks, That should do it..... I forgot i could do that too, but thaks.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!