matlab批量储存变量_[转载]整理:matlab批量读入数据文件的方法

本文介绍了一种在MATLAB中批量读取和处理无规则文件名的数据文件的方法。通过读取含有文件名的'title.txt',然后逐个处理每个文件,进行数据计算,并将结果写入'result.txt'。示例代码中包含了一个用于处理单个文件的辅助函数'numprocessor',该函数加载数据,进行计算并返回结果。
摘要由CSDN通过智能技术生成

主程序:

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 %sn',efc_rst);

fprintf(fidout,'%8.1f %6.2f %6.2f %6.4fn',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

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

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

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

1、MATLAB

clc;clear;

%--将'e:/POP/data/'下的文件信息写入构架数组file_structurefile_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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值