How to combine multiple files into one .csv

3 Ansichten (letzte 30 Tage)
Daphne PARLIARI
Daphne PARLIARI am 10 Mär. 2020
Kommentiert: Daphne PARLIARI am 10 Mär. 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)

Akzeptierte Antwort

Jakob B. Nielsen
Jakob B. Nielsen am 10 Mär. 2020
Bearbeitet: Jakob B. Nielsen am 10 Mär. 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 Kommentare
Jakob B. Nielsen
Jakob B. Nielsen am 10 Mär. 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 am 10 Mär. 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);
"

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Environment and Settings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by