1. 程式人生 > >怎樣用matlab讀取一個資料夾下的多個子資料夾中的多個圖片檔案

怎樣用matlab讀取一個資料夾下的多個子資料夾中的多個圖片檔案

maindir = 'E:\Temp Folder';
subdir =  dir( maindir );   % 先確定子資料夾
 
for i = 1 : length( subdir )
    if( isequal( subdir( i ).name, '.' ) ||  isequal( subdir( i ).name, '..' ) ||  ~subdir( i ).isdir )   % 如果不是目錄跳過
        continue;
    end
    
    subdirpath = fullfile( maindir, subdir( i ).name, '*.jpg' );
    images = dir( subdirpath );   % 在這個子資料夾下找字尾為jpg的檔案
     
    % 遍歷每張圖片
    for j = 1 : length( images )
        imagepath = fullfile( maindir, subdir( i ).name, images( j ).name  )
%         imgdata = imread( imagepath );   % 這裡進行你的讀取操作
    end
end