2017-04-10 1 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 필드에 파일 이름이 있습니다. 그래서 : 당신은 다음 _cellfun_ 사용할 수 있습니다 당신은 파일을 반복 할 필요가 무엇인지에 따라`

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

for 루프에 대한 대안은 '이름 = {files.name}를 사용하여 셀 어레이에 바로 추출하는 것입니다 각 파일 이름을 반복합니다. – Adrian

2

당신은 chars 대신 cells으로이

list=ls('*.png'); 
for ii=1:size(list,1) 
    s = strtrim(list(ii,:)); % a string containing the name of each file 
end 

ls 작품처럼 ls를 사용할 수 있습니다.

관련 문제