吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3657|回复: 2
收起左侧

matlab读取多个excel文件并合并到一个新的excel中

[复制链接]
Luck、Wang 发表于 2020-12-27 15:55
80吾爱币
内容需求:利用matlab读取同一文件夹下的多个EXCEL表格,并将读取后的excel表格内容合并到一个新的excel中。表格样式如图。举例两个,实际上文件中有多个excel文件,都需要读取,然后将读取的内容按顺序的合并到新的excel表格中。
自己尝试了以下代码,但是所有读取的内容都是用A表示矩阵,怎么合并就不知道。M=cat(1,矩阵1,。。。,矩阵N)
path=''D:\shiyan\'';%文件夹路径
de=dir(strcat(path,'*.xlsx'));%列出当前目录下所有子文件夹和文件%
num=length(de)%文件的个数字段
for i=1:num
e=num2str(i)
A=readcell([path,e,'.xlsx'])%读取文件内容
end
q.png
w.png
r.png

最佳答案

查看完整内容

代码如下: [mw_shl_code=text,true] path = '.\'; de = dir(strcat(path, '*.xlsx')); num = length(de); res = {}; for i=1:num name = de(i).name; A = readcell(); if i==1 res = A; else res = [res; A(2:end, :)]; end end % replace missing value to '' mask = cellfun(@ismissing, res, 'UniformOutput', false); % replace 0 array to 0 mask = cellfun(@sum, ma ...

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

zfb 发表于 2020-12-27 15:55
本帖最后由 zfb 于 2020-12-27 18:58 编辑

代码如下:
[Plain Text] 纯文本查看 复制代码
path = '.\';
de = dir(strcat(path, '*.xlsx'));
num = length(de);
res = {};
for i=1:num
    name = de(i).name;
    A = readcell([path, name]);
    if i==1
        res = A;
    else
        res = [res; A(2:end, :)];
    end
end
% replace missing value to ''
mask = cellfun(@ismissing, res, 'UniformOutput', false);
% replace 0 array to 0
mask = cellfun(@sum, mask);
% convert double to logical
mask =  logical(mask);
res(mask) = {''};
writecell(res,'final.xlsx','Sheet','merged datasheet','Range','A1');


效果如下:  

结果

结果


如果你想要把组别这一列,合并单元格并居中,参考链接[How to merge cells in excel](https://www.mathworks.com/matlabcentral/answers/337613-how-to-merge-cells-in-excel)

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
Luck、Wang + 1 + 1 我很赞同!

查看全部评分

 楼主| Luck、Wang 发表于 2020-12-27 19:31
快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-9 03:26

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表