仅使用MATLAB迭代目录中的文件名

时间:2017-04-10 22:46:45

标签: 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.

2 个答案:

答案 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