如何读取多个.dat文件并取平均值?

时间:2012-07-03 09:33:46

标签: matlab file-io

我在文件夹中有100个.dat文件。是否可以使用MATLAB一次读取所有文件并对这100个文件的第5列进行平均分析?以下是其中一个.dat文件的示例。

enter image description here

2 个答案:

答案 0 :(得分:3)

以下是一些可以帮助您入门的代码:

%# get list of 100 .dat files
pathToFolder = '.';
files = dir( fullfile(pathToFolder,'*.dat') );

%# read all files
data = cell(numel(files),1);
for i=1:numel(files)
    fid = fopen(fullfile(pathToFolder,files(i).name), 'rt');
    H = textscan(fid, '%s', 4, 'Delimiter','\n');
    C = textscan(fid, repmat('%f ',1,8), 'Delimiter',' ', ...
        'MultipleDelimsAsOne',true, 'CollectOutput',true);
    fclose(fid);
    H = H{1}; C = C{1};

    %# store numeric data and ignore the header lines
    data{i} = C;
end

%# we assume all tables have the same size
data = cat(3,data{:});
mn = mean(data(:,5,:),3)    %# mean of 5th col across 100 files

答案 1 :(得分:0)

看一下@ this http://www.mathworks.com/matlabcentral/newsreader/view_thread/161967

你的整个问题在这里得到解答。你的回答是FAQ @ matlab

http://matlab.wikia.com/wiki/FAQ

祝你好运!

相关问题