How to combine multiple files into one .csv

2 views (last 30 days)
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)

Accepted Answer

Jakob B. Nielsen
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
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);
Daphne PARLIARI
Daphne PARLIARI on 10 Mar 2020
I got the message:
"Error using xlswrite (line 224)
The specified data range is invalid or too large to write to the specified file format. Try
writing to an XLSX file and use Excel A1 notation for the range argument, for example,
‘A1:D4’.
Error in Tapp (line 175)
xlswrite('C:\PhD\Projects\LIFE
ASTI\C.3\WRF_main_TS_2015_ALL\WRF_Times_ALB+EMISS_FINAL2015\WRF_Thessaloniki_stations_2015\Tapp\Hourly
Stats Tapp All stations.csv',filenames,ranges);
"

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!