Skip folders in a for loop
    9 次查看(过去 30 天)
  
       显示 更早的评论
    
Hi!
I have a month's folder (May) and data for the days in the subfolders as shown. I am using a for loop to read the data of the subfolders. How do I skip the dates 26, 27 and 28?
1 个评论
  Kanishk Singhal
      
 2023-6-28
				You can get a list of files and folders(inside May) using dir function. You can then iterate over that.
If you want to create the folder name using vector you can use,
[13:25 29:31]
采纳的回答
  Stephen23
      
      
 2023-6-28
        
      编辑:Stephen23
      
      
 2023-6-28
  
      "How do I skip the dates 26, 27 and 28?"
I would just use DIR to get a list of content in the parent folder. For example here is one approach:
P = '../../2018/May'; % absolute or relative path to where the parent folder is
S = [dir(fullfile(P,'1*'));dir(fullfile(P,'2*'));dir(fullfile(P,'3*'))];
S(~[S.isdir]) = []; % remove any files
The structure S contains a list of all subfolders in P with names starting with 1, 2, or 3. You can simply loop over that structure, e.g. the 2nd folder listed in S is:
S(2).name
or put all of the folder names into a cell array:
C = {S.name} % optional
Another approach is to use ISFOLDER:
for k = 1:31
    F = fullfile(P,sprintf('%02d',k));
    if isfolder(F)
        ... do whatever
    else
        ... do nothing
    end
end
Note that months also have varying lengths.
0 个评论
更多回答(1 个)
  Manas
      
 2023-6-28
        Hi Indrani,
I am assuming that you are using the fullfile function in matlab to get the addressess of each folder. This function returns a character Vector which has the address of the folder. You can us the If-else statements to skip the folders with date 26,27 and 28.
for more info, you can check out the documentation: 
0 个评论
另请参阅
类别
				在 Help Center 和 File Exchange 中查找有关 File Operations 的更多信息
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



