How to combine multiple files into one .csv
3 次查看(过去 30 天)
显示 更早的评论
Hello everyone!
I would really appreciate your help on the following:
I have some .csv files with the exact same format (Attached you can find two of them) and I want to combine them to one single file . So far I have attempted the following lines of code but when I run them, I only get the contents of the first file, not both of them.
Is there anything I can do??
filenames={'C:\PhD\Projects\LIFE ASTI\C.3\WRF_main_TS_2015_ALL\WRF_Times_ALB+EMISS_FINAL2015\WRF_Thessaloniki_stations_2015\Tapp\Airport\Hourly_Stats Airport Tapp .csv','C:\PhD\Projects\LIFE ASTI\C.3\WRF_main_TS_2015_ALL\WRF_Times_ALB+EMISS_FINAL2015\WRF_Thessaloniki_stations_2015\Tapp\Eptapurgio\Hourly_Stats Eptapurgio Tapp .csv'};
filenames = filenames.';
[~,~,RAW1]=xlsread(filenames{1});
for i=1:size(filenames,1)
[num,~,~]=xlsread(filenames{i});
RAW1=[RAW1;num2cell(num)];
end
xlswrite('C:\PhD\Projects\LIFE ASTI\C.3\WRF_main_TS_2015_ALL\WRF_Times_ALB+EMISS_FINAL2015\WRF_Thessaloniki_stations_2015\Tapp\CombinedFile.csv',RAW1)
0 个评论
采纳的回答
Jakob B. Nielsen
2020-3-10
编辑:Jakob B. Nielsen
2020-3-10
If I run your code and pause it after the first execution of the for loop, the num variable is empty, so something might be up with the file formats? At least, it explains why you only get the first file out, as you append empty arrays to your first file every loop.
I assume you want the header from the first file + the numeric data from the first and second files printed out? This does the trick, although I shudder at the file format ;) it should also work if your input is 100 files instead of 2.
filenames={'C:\PhD\Projects\LIFE ASTI\C.3\WRF_main_TS_2015_ALL\WRF_Times_ALB+EMISS_FINAL2015\WRF_Thessaloniki_stations_2015\Tapp\Airport\Hourly_Stats Airport Tapp .csv','C:\PhD\Projects\LIFE ASTI\C.3\WRF_main_TS_2015_ALL\WRF_Times_ALB+EMISS_FINAL2015\WRF_Thessaloniki_stations_2015\Tapp\Eptapurgio\Hourly_Stats Eptapurgio Tapp .csv'};
filenames = filenames.';
[~,~,RAW]=xlsread(filenames{1});
for i=1:size(filenames,1)
[~,~,all]=xlsread(filenames{i});
RAW{i+1}=all{2};
end
xlswrite('C:\PhD\Projects\LIFE ASTI\C.3\WRF_main_TS_2015_ALL\WRF_Times_ALB+EMISS_FINAL2015\WRF_Thessaloniki_stations_2015\Tapp\CombinedFile.csv',RAW)
5 个评论
Jakob B. Nielsen
2020-3-10
I was more thinking like so:
filenames={'C:\PhD\Projects\LIFE ASTI\C.3\WRF_main_TS_2015_ALL\WRF_Times_ALB+EMISS_FINAL2015\WRF_Thessaloniki_stations_2015\Tapp\Airport\Hourly_Stats Airport Tapp .csv','C:\PhD\Projects\LIFE ASTI\C.3\WRF_main_TS_2015_ALL\WRF_Times_ALB+EMISS_FINAL2015\WRF_Thessaloniki_stations_2015\Tapp\Eptapurgio\Hourly_Stats Eptapurgio Tapp .csv'};
filenames = filenames.';
[~,~,RAW]=xlsread(filenames{1});
ranges=['P2:P',num2str(size(filenames,1))+1];
for i=1:size(filenames,1)
[~,~,all]=xlsread(filenames{i});
RAW{i+1}=all{2};
end
xlswrite('C:\PhD\Projects\LIFE ASTI\C.3\WRF_main_TS_2015_ALL\WRF_Times_ALB+EMISS_FINAL2015\WRF_Thessaloniki_stations_2015\Tapp\CombinedFile.csv',RAW);
ranges=['M2:M',num2str(size(filenames,1))+1];
xlswrite('C:\PhD\Projects\LIFE ASTI\C.3\WRF_main_TS_2015_ALL\WRF_Times_ALB+EMISS_FINAL2015\WRF_Thessaloniki_stations_2015\Tapp\CombinedFile.csv',filenames,ranges);
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!