How to combine multiple files into one .csv
3 views (last 30 days)
Show older comments
Daphne PARLIARI
on 10 Mar 2020
Commented: Daphne PARLIARI
on 10 Mar 2020
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 Comments
Accepted Answer
Jakob B. Nielsen
on 10 Mar 2020
Edited: Jakob B. Nielsen
on 10 Mar 2020
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 Comments
Jakob B. Nielsen
on 10 Mar 2020
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);
More Answers (0)
See Also
Categories
Find more on Environment and Settings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!