【问题标题】:How to read multiple .dat files and take an average?如何读取多个 .dat 文件并取平均值?
【发布时间】:2012-07-03 17:56:06
【问题描述】:

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

【问题讨论】:

  • 是的,这是可能的。到目前为止,您尝试过什么?
  • @Chirs,感谢您的评论,这对我来说是新事物......

标签: matlab file-io


【解决方案1】:

这里有一些代码可以帮助您入门:

%# 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

【讨论】:

    【解决方案2】:

    看看@这个http://www.mathworks.com/matlabcentral/newsreader/view_thread/161967

    您的整个问题都在这里得到解答。你的答案是一个常见问题@matlab

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

    祝你好运!

    【讨论】:

    • 虽然这确实回答了这个问题,it would be preferable 在此处包含答案的基本部分,并提供链接以供参考。
    猜你喜欢
    • 1970-01-01
    • 2014-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-16
    • 2014-07-01
    • 1970-01-01
    相关资源
    最近更新 更多