如何只访问目录中的文件名?
>> 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.
答案 0 :(得分:3)
文件名位于dir
返回的struct数组的字段names
中。所以:
files = dir('*.png');
for k = 1:numel(files)
f = files(k).name; % f contains the name of each file
end
答案 1 :(得分:2)
您可以像这样使用ls
list=ls('*.png');
for ii=1:size(list,1)
s = strtrim(list(ii,:)); % a string containing the name of each file
end
ls
适用于chars
而不是cells
。