使用matlab合并Excel文件

任务描述

有两种不同类型的excel文件,每种共25个。需要将同一种类型文件汇总,并生成新的excel文件。

  • 通过批处理语句copy *type1.xlsx all.xlsx可以实现,但有时候会乱码
  • 练习一下matlab

解决思路

两个matlab程序

  • combine_eyedata.m 读取文件列表;将不同类型文件分类;输出汇总后文件
  • combine_list.m 合并文件程序,以函数形式呈现

combine_eyedata.m

clear;
input_file = 'E:\expdata\exp1\formal\eyecompute';
output_file = 'E:\expdata\exp1\formal\eyecompute_all\';

dirOutput = dir(fullfile(input_file,'*.xlsx'));
file_name_lists = {dirOutput.name};

%
blink_lists = [];
converge_lists = [];
fixation_lists = [];
microsac_lists = [];
pupil_lists = [];
steadiness_lists = [];
for i = 1:length(file_name_lists)
    t = regexp(file_name_lists{i}, '_', 'split');  
    switch t{4}
        case 'blink.xlsx'
            blink_lists = [blink_lists file_name_lists(i)];
        case 'converge.xlsx'
            converge_lists = [converge_lists file_name_lists(i)];
        case 'fixation.xlsx'
            fixation_lists = [fixation_lists file_name_lists(i)];
        case 'microsac.xlsx'
            microsac_lists = [microsac_lists file_name_lists(i)];
        case 'pupil.xlsx'
            pupil_lists = [pupil_lists file_name_lists(i)];
        case 'steadiness.xlsx'
            steadiness_lists = [steadiness_lists file_name_lists(i)];
    end
end

% blink_all = [];
% converge_all = [];
% fixation_all= [];
% microsac_all = [];
% pupil_all= [];
% steadiness_all = [];

blink_all = combine_list(input_file, blink_lists);
converge_all = combine_list(input_file, converge_lists);
fixation_all = combine_list(input_file, fixation_lists);
microsac_all = combine_list(input_file, microsac_lists);
pupil_all = combine_list(input_file, pupil_lists);
steadiness_all = combine_list(input_file, steadiness_lists);

xlswrite([output_file 'blink_all.xlsx'], blink_all);
xlswrite([output_file 'converge_all.xlsx'], converge_all);
xlswrite([output_file 'fixation_all.xlsx'], fixation_all);
xlswrite([output_file 'microsac_all.xlsx'], microsac_all);
xlswrite([output_file 'pupil_all.xlsx'], pupil_all);
xlswrite([output_file 'steadiness.xlsx'], steadiness_all);

combine_list.m

function out_list = combine_list(src, name_list)
    out_list = [];
    for i=1:length(name_list)
        [data, header] = xlsread([src '\' name_list{i}]);
        out_list = [out_list;data];
    end
    out_list = [header; num2cell(out_list)];
end

你可能感兴趣的:(数据处理,matlab)