Reputation: 38145
How to only access the file names in a directory?
>> files = dir('*.png');
>> disp(class(dir('*.png')))
struct
>> fields
fields =
'name'
'date'
'bytes'
'isdir'
'datenum'
>> for i=1:numel(fields)
files.(fields{i}.name)
end
Struct contents reference from a non-struct array object.
>> for i=1:numel(fields)
files.(fields{i}).name
end
Expected one output from a curly brace or dot indexing expression, but there were 11 results.
Upvotes: 0
Views: 74
Reputation: 6424
You can use ls
like this
list=ls('*.png');
for ii=1:size(list,1)
s = strtrim(list(ii,:)); % a string containing the name of each file
end
ls
works with chars
instead of cells
.
Upvotes: 2
Reputation: 112659
File names are in the field names
of the struct array returned by dir
. So:
files = dir('*.png');
for k = 1:numel(files)
f = files(k).name; % f contains the name of each file
end
Upvotes: 3