matlab 批量读入文件夹中的指定文件类型 (目录级数不限)
2012-09-13 17:05
531 查看
比较久以前写的一个批处理文件。测试可用。
可以读入文件中的任意类型文件<理论上是这样。O(∩_∩)O~现在主要是图片格式和txt,还有csv这几种格式,具体的大家可以再加>。不限文件夹中子文件夹的个数和层数。
ps一个,文件中原本打算设计可以批量写成文件。但是这部分想来似乎不是很有用。所以就没有做。
如果大家要对读入的数据进行处理,可以在%load function处添加你要的function即可。其输入参数使用Output_Data。
大家先看看,有问题,一起讨论一下。
可以读入文件中的任意类型文件<理论上是这样。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
大家先看看,有问题,一起讨论一下。
相关文章推荐
- matlab遍历文件夹下指定类型的文件以及子文件夹
- 从文件夹目录下导入所有指定文件类型的数据内容到数据库表
- Python 批量删除文件夹下面指定类型的所有文件
- 批量删除目录下指定类型文件
- 使用CMD模式批量删除指定目录(支持目录中多层文件夹嵌套)下指定格式文件
- matlab代码-遍历文件夹中指定类型的文件
- Python批量删除指定目录下的指定类型的文件
- Python批量删除指定文件夹下的指定类型的文件
- Python 批量删除文件夹下面指定类型的所有文件
- 批量删除当前目录下所有文件夹中的指定文件
- matlab中怎样读取一个文件夹里面的指定类型的所有文件的名称?
- Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)(二)
- C# 删除指定目录下的 所有指定文件、文件夹
- SHELL编程练习-获得指定目录下的所有文件及文件夹的大小
- Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)
- 以树状形式显示指定目录下的文件和文件夹
- python实现批量获取指定文件夹下的所有文件的厂商信息
- [转]Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)
- python实现指定目录下批量文件的单词计数:串行版本
- VC下读取文件夹目录 + 读取该目录下所有指定文件 + 把所有读取结果输出到CListBox并智能设置水平滚动条宽度