combine or merge multiple excel files into one file.
1 次查看(过去 30 天)
显示 更早的评论
hi, i have 100 excel files and they have one of the three following types.
type1:
name id 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019
david a
james a
tome a
alim a
type2:
name id 2010 2011 2012 2013 2014 2015 2016 2017 2018
david a
james a
tome a
alim a
type3:
name id 2011 2012 2013 2014 2015 2016 2017 2018 2019
david a
james a
tome a
alim a
is there any way that i can combine all of the 100 files into one by using simple loop or any other feasible way?.
Thanks
0 个评论
采纳的回答
Walter Roberson
2020-3-29
name = cell(0,0); id = cell(0,0);
x2010 = []; x2011 = []; x2012 = []; x2013 = []; x2014 = []; x2015 = []; x2016 = []; x2017 = []; x2018 = []; x2019 = [];
result = table(name, id, x2010, x2011, x2012, x2013, x2014, x2015, x2016, x2017, x2018, x2019);
dinfo = dir('*.xlsx');
nfiles = length(dinfo);
for K = 1 : nfiles
thistable = readtable(result, 'readvariablenames', true);
thisvars = thistable.Properties.VariableNames;
if ~ismember( 'x2010', thisvars)
thistable.x2010 = nan(height(thistable),1);
end
if ~ismember( 'x2019', thisvars)
thistable.x2019 = nan(height(thistable,1));
end
result = [result; thistable]; %will automatically rearrange variables to match first table
end
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Exploration 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!