Matlab如何批量读入文件

    xiaoxiao2021-03-25  95

    网上搜集的几个代码,很有用,保存。

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    1)要读入的文件下的文件名称依序列的方式命名,如a1b.mat, a2b.mat,...,ajb.mat,...

       循环读入

       filepath='';%文件夹的路径

       for i=1:n  %n是要读入的文件的个数

           load([filepath 'a' num2str(i) 'b' '.mat'])

       end

     

    2)文件夹下的文件名称无规律

        如,文件夹里是n幅图像(.jpg) 和一些数据(其他类型),现在要读出所有的图像

            a,先得到文件路径                 di = dir('文件路径\*.jpg');          b,读入              for k= 1:length(di)                      I(k,:,:) = imread(['文件路径',di(k).name]);               end +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    主程序:

     

    clc; clear; fidin=fopen('title.txt','r');

    fidout=fopen('result.txt','w');

    while ~feof(fidin)                  %while ~feof 表示 若 未读到文件末尾 则 继续 循环     wellname=fgetl(fidin);          %如果没有使用fgetl函数,则feof判断没有作用,这将是一个死循环!     titleline=strcat(wellname,'.txt');     [efc_dpt,efc_poro,efc_perm,efc_ratio]=numprocessor(titleline);     efc_rst=[efc_dpt,efc_poro,efc_perm,efc_ratio];     fprintf(fidout,wellname);     %fprintf(fidout,'%s %s %s %s\n',efc_rst);     fprintf(fidout,'%8.1f %6.2f %6.2f %6.4f\n',efc_rst); end fclose(fidin); fclose(fidout);

     

    部分文件名(title.txt)

    B12-B51-58 B12-B53-58 B12-B55-59 B12-B55-62 B12-B55-64 B12-B57-51 B14-B50-44 B14-B50-48 B14-B51-46 B14-B52-49 B14-B54-48 B14-B54-53 B14-B54-74 B14-B55-52 B14-B55-56 B14-B55-60 B14-B55-63 B14-B55-65 B14-B55-67 B14-B55-69 B14-B55-75 B14-B56-49 B14-B56-53 B14-B56-70

    …………

     

    function [efc_dpt,efc_poro,efc_perm,efc_ratio]=numprocessor(file) %读入数据,进行处理,输出结果     matdat=load(file);     [n,l]=size(matdat);         efc_dpt=0;     efc_perm=0;     efc_poro=0;     efc_ratio=0;         for i=1:n-1         matdat(i,1)=matdat(i+1,1)-matdat(i,1);     end         for i=1:n-1         if matdat(i,3)==1             efc_dpt=efc_dpt+matdat(i,1);             efc_perm=efc_perm+matdat(i,5)*matdat(i,1);             efc_poro=efc_poro+matdat(i,4)*matdat(i,1);             if matdat(i,2)==0                 efc_ratio=efc_ratio+matdat(i,1);             end         end     end     if efc_dpt==0         efc_perm=0;         efc_poro=0;         efc_ratio=0;

        else         efc_perm=efc_perm/efc_dpt;         efc_poro=efc_poro/efc_dpt;         efc_ratio=efc_ratio/efc_dpt;        end

     

    井的数据

    1865.2 3 0 0 0 1867.8 0 1 6.8 0.1 1874.4 3 0 0 0 1885 1 1 3.3 0.02 1888.8 3 0 0 0 1891.8 0 1 2.3 0.03 1897 3 0 0 0 1898 0 1 2.3 0.03 1903 3 0 0 0 1906.2 0 1 2.3 0.03 1911 3 0 0 0 1914.6 1 1 3.4 0.21 1919 3 0 0 0

    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    多数据文件批处理的一个技巧

    数据处理过程中,很多时候需要对大批的数据文件进行循环处理。如果文件名是有规律的,那么正常的方法尚可处理,但是碰到文件名没什么规律,或规律不强时,表示出这些文件按名就是一件很头疼的事了。假设现有以下文件需要进行循环处理,以供参考。

    1MATLAB  clc;clear;  %--'e:/POP/data/'下的文件信息写入构架数组file_structure file_structure = dir('e:/POP/data/');  %--file_structure.name下是'e:/POP/data/'内的文件名 for i=3:size(file_structure)      

      filename = strcat('e:/POP/data/',file_structure(i).name)                 % stract 连接字符串   f=netcdf(filename,'nowrite');     a=['在此对f及里面的变量操作...']     close(f) end

        ----------------------------------------------------- 怎么用matlab读取多个数据文件? 1:如果文件名规则 % 文件目录: my_dir=' /home/my_calculation1/test1/'; %文件名前缀 my_pre_T='test' ; %文件数目 fnum =1:32; %Read files for i=1:length(fnum) filename = [my_dir,prefix_T1 num2str(fnum(i))]; 然后dlmread,fopen等操作就不需详写了!与单个文件一样。 end   2:文件夹数据批量读取的问题,命名无规则 在windows下很简单,ls函数便可以得到一个包含所有文件名的字符矩阵。可惜在unix下得到的是个字符行向量。当然,处理下也可以批量读取数据文件,不过不想再写了。因为另外一个函数也可以实现批量处理数据文件的功能。即dir函数 LS displays the results of the 'ls' command on UNIX.  On UNIX, LS returns acharacter row vectorof filenames separated by tab and space characters.  假定.dat文件在/home/my_calculation1/test1/ 文件夹下,文件名符合matlab变量名的命名规则,要读取第二列的浮点数字为数组并以文件名为变量名。 mydir='/home/my_calculation1/test1/'; temp1=dir([mydir,'*.dat']); temp=dlmread(filename,'',0,1); 1、利用dir(外层文件夹)获取子文件夹;2、利用dir(子文件夹)获取子文件夹中的文件信息;3、遍历文件,做处理; 对于遍历文件夹,也可以使用mathworks网站上的函数dirr轻松搞定。 注意:如果直接使用 temp1=dir(mydir]);读取文件夹下所有文件,应当从第三个开始才是目录下的文件。 另外,使用dir还可以遍历一个文件夹下的所有子文件夹。 eval_r([temp1(i1).name(1:end-4),'=temp;']) dlmread,fopen等操作就不需详写了!与单个文件一样。 end num_temp1=length(temp1); for i1=1:num_temp1 filename=[mydir,temp1(i1).name]; 现在有一个文件夹里面有50个的.dat文件.每个文件大概三万行.两列,第一列是字符串,第二列是浮点数字. 批量读取这个文件夹里的.dat文件的第二列。读取完以后的数组变量名就是.dat的文件名 On Windows, LS returns anm-by-n character array of filenames, where m is the number of filenames and n is the number of characters in the longest filename found. Filenames shorter than n characters are padded with space characters. 下面介绍下使用dir函数读取文件夹中文件的程序 2.1 读取所有扩展名为.dat的数据文件,并进行处理。 matlab中,也可以使用通配符的。下面就来展示下:
    转载请注明原文地址: https://ju.6miu.com/read-19216.html

    最新回复(0)