您的位置:首页 > 编程语言 > MATLAB

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

2012-09-13 17:05 531 查看
比较久以前写的一个批处理文件。测试可用。

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

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

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

function [ Output_data ] = File_Batch_Processing( Input_path  , File_type  )
%bat_file has agrments as follow:
%1. the folder where you want to read or write. agrments *Input_path
%2. the type you want to , generally, read or write. *Operate_mothed
%3. the file type you want to , currently , is txt,jpg,bmp,dat , and so
%on.*File-type
%4. the perfix name of the writing file.*File_type
%Read Mode
if nargin <= 2%input agruments for this mode is @Input_path and @File_type
Is_Empty_Directory_Flag = 0;
Mother_Directory = dir(Input_path);
Directory_Name = {Mother_Directory};
Directory_Path = {Input_path};

while(Is_Empty_Directory_Flag == 0)
Directory_Son_Count = 1;
for Directory_Name_Count = 1 : length(Directory_Name)
for Directory_File_Count = 3 : length(Directory_Name{Directory_Name_Count})
if Directory_Name{Directory_Name_Count}(Directory_File_Count).isdir == 1    %this is a Directory
Directory_Son{Directory_Son_Count} = [Directory_Path{Directory_Name_Count} Directory_Name{Directory_Name_Count}(Directory_File_Count).name '\'];
Directory_Son_Count = Directory_Son_Count + 1;
elseif Directory_Name{Directory_Name_Count}(Directory_File_Count).isdir == 0   %this is a File
File_Path_And_Name = [Directory_Path{Directory_Name_Count} Directory_Name{Directory_Name_Count}(Directory_File_Count).name];
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
%Load function here.
end
end
end
clear Directory_Path;
clear Directory_Name;
if ~exist('Directory_Son' , 'var')
Is_Empty_Directory_Flag = 1;
else
Is_Empty_Directory_Flag = 0;
for Directory_Son_Count_Loop = 1 : Directory_Son_Count - 1
Directory_Path{Directory_Son_Count_Loop} = Directory_Son{Directory_Son_Count_Loop};
Directory_Name{Directory_Son_Count_Loop} = dir(Directory_Son{Directory_Son_Count_Loop}(1,:));
end
end
clear Directory_Son;
end

%Write Mode
%elseif nargin >= 3

end
end

function Output_data = File_read_in(File_Path_and_Name)
switch(File_Path_and_Name.name(end-2:end))
case{'txt'}
Output_data = Txt_Read_In(File_Path_and_Name);
case{'png'}
Output_data = Img_Read_In(File_Path_and_Name);
case{'bmp'}
Output_data = Img_Read_In(File_Path_and_Name);
case{'jpg'}
Output_data = Img_Read_In(File_Path_and_Name);
case{'dcm'}
Output_data = Dicom_Read_In(File_Path_and_Name);
case{'csv'}
Output_data = Excel_Read_In(File_Path_and_Name);
end
end

function Output_Data = Txt_Read_In(File_Path_and_Name)          %read in the txt as a in Row.
fid = fopen(File_Path_and_Name);
Output_Data = fread(fid);
fclose(fid);
end

function Output_Data = Img_Read_In(File_Path_and_Name)          %read in the image.
Output_Data = imread(File_Path_and_Name);
end

function Output_Data = Excel_Read_In(File_Path_and_Name)          %read in the excel.the data was collecting in a row
Output_Data = xlsread(File_Path_and_Name);
end

function Output_Data = Dicom_Read_In(File_Path_and_Name)          %read in the excel.the data was collecting in a row
Output_Data = xlsread(File_Path_and_Name);
end


大家先看看,有问题,一起讨论一下。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐