matlab文件读写处理实例(二)——textread批量读取文件
问题:对文件夹下所有文件进行批量读取,跳过文件头部分,读取每个文件数据部分的7,8,9列,保存到变量并且输出到文件。
数据:
文件夹11m\
单个文件格式:
DAV1 MARKER NAME
66010M001 MARKER NUMBER
7 PR TD HR WS WD RI HI # / TYPES OF OBSERV
PAROSCIENTIFIC MET4 0.1 PR SENSOR MOD/TYPE/ACC
PAROSCIENTIFIC MET4 0.5 TD SENSOR MOD/TYPE/ACC
PAROSCIENTIFIC MET4 2.0 HR SENSOR MOD/TYPE/ACC
0.0 WS SENSOR MOD/TYPE/ACC
0.0 WD SENSOR MOD/TYPE/ACC
0.0 RI SENSOR MOD/TYPE/ACC
0.0 HI SENSOR MOD/TYPE/ACC
486854.5000 2285099.3660 -5914955.7710 44.7169 PR SENSOR POS XYZ/H
486854.5000 2285099.3660 -5914955.7710 44.7169 TD SENSOR POS XYZ/H
486854.5000 2285099.3660 -5914955.7710 44.7169 HR SENSOR POS XYZ/H
END OF HEADER
11 2 1 0 0 1 988.4 -1.0 93.0 0.0 0.0 0.0 0.0
11 2 1 0 0 31 988.4 -1.0 93.1 0.0 0.0 0.0 0.0
11 2 1 0 1 1 988.3 -1.0 93.1 0.0 0.0 0.0 0.0
11 2 1 0 1 31 988.3 -1.0 92.9 0.0 0.0 0.0 0.0
11 2 1 0 2 1 988.3 -1.0 93.0 0.0 0.0 0.0 0.0
11 2 1 0 2 31 988.3 -1.0 93.0 0.0 0.0 0.0 0.0
11 2 1 0 3 1 988.3 -1.0 93.0 0.0 0.0 0.0 0.0
11 2 1 0 3 31 988.3 -1.0 93.2 0.0 0.0 0.0 0.0
11 2 1 0 4 1 988.3 -1.0 93.1 0.0 0.0 0.0 0.0
11 2 1 0 4 31 988.3 -1.0 92.8 0.0 0.0 0.0 0.0
11 2 1 0 5 1 988.3 -1.0 92.8 0.0 0.0 0.0 0.0
11 2 1 0 5 31 988.3 -1.1 92.6 0.0 0.0 0.0 0.0
11 2 1 0 6 1 988.3 -1.0 92.7 0.0 0.0 0.0 0.0
11 2 1 0 6 31 988.3 -1.0 92.8 0.0 0.0 0.0 0.0
11 2 1 0 7 1 988.3 -1.0 93.0 0.0 0.0 0.0 0.0
66010M001 MARKER NUMBER
7 PR TD HR WS WD RI HI # / TYPES OF OBSERV
PAROSCIENTIFIC MET4 0.1 PR SENSOR MOD/TYPE/ACC
PAROSCIENTIFIC MET4 0.5 TD SENSOR MOD/TYPE/ACC
PAROSCIENTIFIC MET4 2.0 HR SENSOR MOD/TYPE/ACC
0.0 WS SENSOR MOD/TYPE/ACC
0.0 WD SENSOR MOD/TYPE/ACC
0.0 RI SENSOR MOD/TYPE/ACC
0.0 HI SENSOR MOD/TYPE/ACC
486854.5000 2285099.3660 -5914955.7710 44.7169 PR SENSOR POS XYZ/H
486854.5000 2285099.3660 -5914955.7710 44.7169 TD SENSOR POS XYZ/H
486854.5000 2285099.3660 -5914955.7710 44.7169 HR SENSOR POS XYZ/H
END OF HEADER
11 2 1 0 0 1 988.4 -1.0 93.0 0.0 0.0 0.0 0.0
11 2 1 0 0 31 988.4 -1.0 93.1 0.0 0.0 0.0 0.0
11 2 1 0 1 1 988.3 -1.0 93.1 0.0 0.0 0.0 0.0
11 2 1 0 1 31 988.3 -1.0 92.9 0.0 0.0 0.0 0.0
11 2 1 0 2 1 988.3 -1.0 93.0 0.0 0.0 0.0 0.0
11 2 1 0 2 31 988.3 -1.0 93.0 0.0 0.0 0.0 0.0
11 2 1 0 3 1 988.3 -1.0 93.0 0.0 0.0 0.0 0.0
11 2 1 0 3 31 988.3 -1.0 93.2 0.0 0.0 0.0 0.0
11 2 1 0 4 1 988.3 -1.0 93.1 0.0 0.0 0.0 0.0
11 2 1 0 4 31 988.3 -1.0 92.8 0.0 0.0 0.0 0.0
11 2 1 0 5 1 988.3 -1.0 92.8 0.0 0.0 0.0 0.0
11 2 1 0 5 31 988.3 -1.1 92.6 0.0 0.0 0.0 0.0
11 2 1 0 6 1 988.3 -1.0 92.7 0.0 0.0 0.0 0.0
11 2 1 0 6 31 988.3 -1.0 92.8 0.0 0.0 0.0 0.0
11 2 1 0 7 1 988.3 -1.0 93.0 0.0 0.0 0.0 0.0
CODE:
clear;
clc;
dirpath=input(\'Please input files path:\n\',\'s\');
list=dir(dirpath);
len=length(list)
h=44.5;
j=1;
fid = fopen(\'result.txt\',\'w\');
for i=3:len
filepath=[dirpath \'\\' list(i).name]
[a b c] = textread(filepath,\'%*d %*d %*d %*d %*d %*d %f %f %f %*f %*f %*f %*f\',\'headerlines\',17) ;
m=1e-6*77.6*a/(b+273.15)*(40136+148.73*b-h)/5*1000;
n=1e-6*(374700+273*(71.6-77.6))*(c/100*6.11.*10.^(7.5.*b./(b+273.15)))./(b+273.15).^2*(11000-h)/5*1000;
name=repmat(filepath(10:12),length(m),1);
fprintf(fid,\'%3s %.6f %.6f %.6f\n\',filepath(9:11),nanmean(m),nanmean(n),nanmean(m+n));
result(j,:)={name m n m+n};
j=j+1;
end
clc;
dirpath=input(\'Please input files path:\n\',\'s\');
list=dir(dirpath);
len=length(list)
h=44.5;
j=1;
fid = fopen(\'result.txt\',\'w\');
for i=3:len
filepath=[dirpath \'\\' list(i).name]
[a b c] = textread(filepath,\'%*d %*d %*d %*d %*d %*d %f %f %f %*f %*f %*f %*f\',\'headerlines\',17) ;
m=1e-6*77.6*a/(b+273.15)*(40136+148.73*b-h)/5*1000;
n=1e-6*(374700+273*(71.6-77.6))*(c/100*6.11.*10.^(7.5.*b./(b+273.15)))./(b+273.15).^2*(11000-h)/5*1000;
name=repmat(filepath(10:12),length(m),1);
fprintf(fid,\'%3s %.6f %.6f %.6f\n\',filepath(9:11),nanmean(m),nanmean(n),nanmean(m+n));
result(j,:)={name m n m+n};
j=j+1;
end
请发表评论