2017-04-10 57 views
0

如何訪問目錄中的文件名?只通過使用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. 

回答

3

文件名位於由dir返回的結構數組的字段names中。所以:

files = dir('*.png'); 
for k = 1:numel(files) 
    f = files(k).name; % f contains the name of each file 
end 
+0

到另一種for循環是通過使用'名稱= {} files.name直接提取到一個單元陣列;'根據你的需要遍歷文件就可以使用_cellfun_遍歷每個文件名。 – Adrian

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