只通过使用MATLAB的目录中的文件名迭代
问题描述:
如何访问目录中的文件名?只通过使用MATLAB的目录中的文件名迭代
>> 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.
答
文件名位于由dir
返回的结构数组的字段names
中。所以:
files = dir('*.png');
for k = 1:numel(files)
f = files(k).name; % f contains the name of each file
end
答
您可以使用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
。
到另一种for循环是通过使用'名称= {} files.name直接提取到一个单元阵列;'根据你的需要遍历文件就可以使用_cellfun_遍历每个文件名。 – Adrian