MATLAB: I need help please.

folder reading

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)

Best Answer

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);
...