【问题标题】:Reading multiple Excel files, averaging, and writing to a single Excel file in Matlab在 Matlab 中读取多个 Excel 文件、求平均值并写入单个 Excel 文件
【发布时间】:2016-11-01 17:26:24
【问题描述】:

我正在尝试扩展我编写的一些代码。在下面包含该脚本可能会很有用:

% importing single excel sheet

data = xlsread('test_file.xlsx');

% averaging durations (exluding NaNs)

average_rx_time = mean(reaction_time, 'omitnan');
average_reach_dur = mean(reach_dur, 'omitnan');
average_lift_dur = mean(lift_dur, 'omitnan');
average_hold_dur = mean(hold_dur, 'omitnan');
average_withdrawal_dur = mean(withdrawal_dur, 'omitnan');

% Excel file output containing daily averages

a = [average_rx_time, average_reach_dur, average_lift_dur, average_hold_dur, average_withdrawal_dur];
data_cells = num2cell(a);
column_headers ={'Rx Time', 'Reach Dur', 'Lift Dur', 'Hold Dur', 'Withdrawal Dur'};
row_headers(1,1) ={'Day 1'};
output = [{' '} column_headers; row_headers data_cells];
xlswrite('Test.xls', output);

这部分有效。它读取单个 Excel 工作表中的一堆值,平均一些数字,然后将这些平均值打印到另一个 Excel 工作表。我现在需要做的是从一个目录中读取几个文件(它们都存在一个文件夹中并且是相同的文件类型),平均每个文件中的相同值,然后在电子表格中使用它们各自的文件名打印它们。

我认为我应该使用循环,但我不确定在哪里实现它。我也不确定如何在打印到同一个 Excel 文件时读取多个 Excel 文件。有什么想法吗?

谢谢, 米奇

【问题讨论】:

    标签: excel matlab


    【解决方案1】:

    使用cddir 函数获取指定文件夹中的文件列表:

    cd('\path\to\folder');    % set MATLAB's current folder
    fileList = dir('*.xlsx'); % get list of .xlsx files in a struct array
    
    for fileNum = 1:length(fileList)
        fileName = fileList(fileNum).name;
    
        % do whatever you want with fileName here
    
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多