I write a code for reading folders from folder.In one folder have 300 subfolder. when i read subfolder it read first folder and read 10 forlder and read 100th folder it skip the folder. Code is here please help me finding error.
main_folder = uigetdir( 'E:\Program Files\MATLAB\');
PT=fullfile(main_folder);
main_srcFiles = dir(PT);
main_srcFiles(1:2)=[];
for i=1:length(main_srcFiles)
sub_folder=(main_srcFiles(i).name); Have mistake here.it can't read folder in sequence
pt=strcat(PT,'\',sub_folder);
Path=fullfile(pt);
srcFiles = dir(Path)

 Risposta accettata

Jan
Jan il 9 Ago 2017
Modificato: Jan il 9 Ago 2017
The code does not contain an error. You do read the files in the correct order, but note, that this is the alphabetical order. Then this is expected: 'folder1', 'folder10', 'folder100', 'folder2', ...
To solve this either use folder names like 'folder0001', or a tool for a "natural sorting", e.g.
[EDITED] With a code example:
There is no need to apply fullfile for a single argument only. But if you have 2 parts, do not use strcat. Better: main_folder = uigetdir( 'E:\Program Files\MATLAB\');
PT = uigetdir('E:\Program Files\MATLAB\');
PT_dir = dir(PT);
PT_dir(1:2) = [];
PT_dir = main_srcFiles([PT_dir.isdir]); % Folders only
PT_names = natsortfiles({PT_dir.name});
for i = 1:length(PT_names)
sub_folder = PT_names{i};
FilePath = fullfile(PT, sub_folder); % Not STRCAT
srcFiles = dir(FilePath);
...

5 Commenti

Shagufta Abid
Shagufta Abid il 9 Ago 2017
my folder name are numeric in order 1,2,3,....300 like this
...
sub_folder=(main_srcFiles(i).name); Have mistake here.it can't read folder in sequence
pt=strcat(PT,'\',sub_folder);
But there's no guarantee subfolders will be first in the directory order; must ensure is a directory either by
  1. a specific search with a wild card pattern known to match the names of subdirectories wanted (often '*.' works to return only names with no extensions, but that relies on nobody having named a file without) or
  2. use the isdir logical field of the structure to check.
When do that, then need a nested loop to first iterate over files inside the outer loop over directories.
Jan
Jan il 9 Ago 2017
Modificato: Jan il 9 Ago 2017
@Shagufta Abid: No, the files are not stored on the disk on a particular order and even the order after the import by dir is not documented. All we know, is that the files are replied in an alphabetical order in all tests, such that we can assume, that this is done internally. And the alphabetical order of the files called '1', '2', ..., '300' is:
'1', '10', '100', '2', '20', '200', '3', '30', '300', '4', '5', ...
This is not the numerical order, but the posted tools convert the strings and consider the numerical order to get:
'1', '2', '3', ..., '9', '10', '11', ... '300'
Clear now?
For dpb's comment see the the suggested code under [EDITED].
Shagufta Abid
Shagufta Abid il 9 Ago 2017
"natsortfiles" this is not working in my matlab 2017a. when run code there is error undefined function
You need to download it from the FileExchange at first. I've posted the link already. Then store the file in a folder, which is included in Matlab's path. See:
doc addpath

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Environment and Settings in Centro assistenza e File Exchange

Richiesto:

il 9 Ago 2017

Commentato:

Jan
il 9 Ago 2017

Community Treasure Hunt

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

Start Hunting!

Translated by