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


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

    可以读入文件中的任意类型文件<理论上是这样。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

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

  • 相关阅读:
    最简单跨平台的日志库
    linux文件锁
    Linux 获取屏幕分辨率与窗口行列数(c/c++)
    linux 信号机制
    记一次函数异常(getopt_long)
    程序单实例运行
    简单地 Makefile 书写
    学习go的一些笔记
    20200930 10. Netty 核心源码剖析
    20200930 9. TCP 粘包和拆包 及解决方案
  • 原文地址:https://www.cnblogs.com/zhongnanshan/p/2683664.html
Copyright © 2020-2023  润新知