MATLAB读取一个文件夹下的多个子文件夹中的多个指定格式的文件

 目标:MATLAB需要读取一个文件夹下的多个子文件夹中的指定格式文件,这里以读取*.JPG格式的文件为例

 1、首先确定包含多个子文件夹的总文件夹

1 maindir = 'C:\Temp Folder';

  2、再确定有哪些子文件夹,并过滤掉干扰的文件

1 subdir =  dir( maindir );   % 确定子文件夹  
2 for i = 1 : length( subdir ) 
3 if( isequal( subdir( i ).name, '.' ) ||  isequal( subdir( i ).name, '..' ) || ~subdir( i ).isdir )   % 如果不是目录跳过 
4 continue; 
5 end 

  3、找出子文件中的目标文件

1 subdirpath = fullfile( maindir, subdir( i ).name, '*.jpg' ); 
2 images = dir( subdirpath );   % 在这个子文件夹下找后缀为jpg的文件 

    4、对目标文件进行读取

1 for j = 1 : length( images ) 
2 imagepath = fullfile( maindir, subdir( i ).name, images( j ).name) 
3 end 
posted @ 2019-08-26 19:38  Sunny_SunShine  阅读(14506)  评论(2编辑  收藏  举报