• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

matlab批量读入文件夹中的指定文件类型(目录级数不限)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

比较久以前写的一个批处理文件。测试可用。

可以读入文件中的任意类型文件<理论上是这样。O(∩_∩)O~现在主要是图片格式和txt,还有csv这几种格式,具体的大家可以再加>。不限文件夹中子文件夹的个数和层数。

ps一个,文件中原本打算设计可以批量写成文件。但是这部分想来似乎不是很有用。所以就没有做。

如果大家要对读入的数据进行处理,可以在%load function处添加你要的function即可。其输入参数使用Output_Data。

 1 function [ Output_data ] = File_Batch_Processing( Input_path  , File_type  )
 2 %bat_file has agrments as follow: 
 3 %1. the folder where you want to read or write. agrments *Input_path
 4 %2. the type you want to , generally, read or write. *Operate_mothed
 5 %3. the file type you want to , currently , is txt,jpg,bmp,dat , and so
 6 %on.*File-type
 7 %4. the perfix name of the writing file.*File_type
 8 %Read Mode
 9 if nargin <= 2%input agruments for this mode is @Input_path and @File_type
10     Is_Empty_Directory_Flag = 0;
11     Mother_Directory = dir(Input_path);
12     Directory_Name = {Mother_Directory};
13     Directory_Path = {Input_path};
14     
15     while(Is_Empty_Directory_Flag == 0)
16         Directory_Son_Count = 1;
17         for Directory_Name_Count = 1 : length(Directory_Name)
18             for Directory_File_Count = 3 : length(Directory_Name{Directory_Name_Count})
19                 if Directory_Name{Directory_Name_Count}(Directory_File_Count).isdir == 1    %this is a Directory
20                     Directory_Son{Directory_Son_Count} = [Directory_Path{Directory_Name_Count} Directory_Name{Directory_Name_Count}(Directory_File_Count).name '\'];
21                     Directory_Son_Count = Directory_Son_Count + 1;
22                 elseif Directory_Name{Directory_Name_Count}(Directory_File_Count).isdir == 0   %this is a File
23                    File_Path_And_Name = [Directory_Path{Directory_Name_Count} Directory_Name{Directory_Name_Count}(Directory_File_Count).name];
24                     Output_data = File_read_in(File_Path_And_Name,File_type);%read in the file name and file type , here insert some functions as well
25                     %Load function here. 
26                 end
27             end            
28         end
29         clear Directory_Path;
30         clear Directory_Name;
31         if ~exist('Directory_Son' , 'var')
32             Is_Empty_Directory_Flag = 1;
33         else
34             Is_Empty_Directory_Flag = 0;
35             for Directory_Son_Count_Loop = 1 : Directory_Son_Count - 1
36                 Directory_Path{Directory_Son_Count_Loop} = Directory_Son{Directory_Son_Count_Loop};
37                 Directory_Name{Directory_Son_Count_Loop} = dir(Directory_Son{Directory_Son_Count_Loop}(1,:));
38             end
39         end
40         clear Directory_Son;
41     end
42             
43     
44     
45 %Write Mode    
46 %elseif nargin >= 3
47     
48     
49     
50 end
51 end
52 
53 
54 function Output_data = File_read_in(File_Path_and_Name)
55             switch(File_Path_and_Name.name(end-2:end))
56                 case{'txt'}
57                     Output_data = Txt_Read_In(File_Path_and_Name);
58                 case{'png'}
59                     Output_data = Img_Read_In(File_Path_and_Name);
60                 case{'bmp'}
61                     Output_data = Img_Read_In(File_Path_and_Name);
62                 case{'jpg'}
63                     Output_data = Img_Read_In(File_Path_and_Name);
64                 case{'dcm'}
65                     Output_data = Dicom_Read_In(File_Path_and_Name);   
66                 case{'csv'}
67                     Output_data = Excel_Read_In(File_Path_and_Name);
68             end
69 end
70 
71 function Output_Data = Txt_Read_In(File_Path_and_Name)          %read in the txt as a in Row.
72 fid = fopen(File_Path_and_Name);
73 Output_Data = fread(fid);
74 fclose(fid);
75 end
76 
77 function Output_Data = Img_Read_In(File_Path_and_Name)          %read in the image.
78 Output_Data = imread(File_Path_and_Name);
79 end
80 
81 function Output_Data = Excel_Read_In(File_Path_and_Name)          %read in the excel.the data was collecting in a row
82 Output_Data = xlsread(File_Path_and_Name);
83 end
84 
85 function Output_Data = Dicom_Read_In(File_Path_and_Name)          %read in the excel.the data was collecting in a row
86 Output_Data = xlsread(File_Path_and_Name);
87 end

大家先看看,有问题,一起讨论一下。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
MATLAB-plot、fplot和ezploy函数总结发布时间:2022-07-18
下一篇:
delphi之IOCP学习(一)发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap