MATLAB: How to set the no if iterations of a for loop dynamically

dynamic number if iterationsfor loop

p=dir % Takes all the folders in the diractory
j=1 % flag variable
i= % iteration vaaiable
a=zeros(1,3) % value associated witht eh flag variable
a(1,j)=length(p)
for i=1:1:a(1,j)
if strcmp(( p(i).name),'.')==1
p(i)=[];
j=j+1
else
continue
end
a(1,j)=length(p);
end

Best Answer

The MATLAB approach is to do these two steps before the loop:
  1. create a cell array of the folder names
  2. use setdiff or ismember to remove '.' and '..' folders
P = dir(); % entire folder contents
C = {P([P.isdir]).name}; % just folder names
C = setdiff(C,{'..','.'}) % remove '.' and '..'
for k = 1:numel(C)
F = C{k};
... do something with folder F
end