Unknown File/Folder in directory

11 ビュー (過去 30 日間)
Mir AbdulRehman
Mir AbdulRehman 2021 年 1 月 23 日
編集済み: per isakson 2021 年 1 月 24 日
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 件のコメント
Stephen23
Stephen23 2021 年 1 月 24 日
編集済み: Stephen23 2021 年 1 月 24 日
The simple way to ignore the '.' and '..' folders:
S = dir(Folder_Name);
S = S(~ismember({d.name},{'.','..'}));

サインインしてコメントする。

採用された回答

per isakson
per isakson 2021 年 1 月 23 日
編集済み: per isakson 2021 年 1 月 24 日
"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 件のコメント
Image Analyst
Image Analyst 2021 年 1 月 23 日
if strcmp(image_path, '.') || strcmp(image_path, '..')
continue; % Skip to bottom of loop.
end
Mir AbdulRehman
Mir AbdulRehman 2021 年 1 月 24 日
Thanks, That should do it..... I forgot i could do that too, but thaks.

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by