telemachos
telemachos

Reputation: 151

Matlab: select elements of vector conditionally

I would like to get a list with all files in a directory

 d=dir(pwd); # get a struct of all elements (including folders)

now i would like to select all element of d.name, where d.isfolder=0

 jj=1;
 for ii=1:numel(d)
   if d(ii).isdir==0
     filelist{jj} = d(ii).name;
     jj+1;
   end
 end

Is this possible without a loop? I think there should by a way to vectorize this operation

i'm working with matlab/octave

Upvotes: 1

Views: 145

Answers (1)

slayton
slayton

Reputation: 20319

No need to use a loop, you can do this with indexing

isDirIdx = [d.isdir];
names = {d.name};
fileNames = names(~isDirIdx);

Upvotes: 2

Related Questions