Assign names to excel sheets with respet to the original filenames?

1 Ansicht (letzte 30 Tage)
Hello. I hope everyone is safe and healthy during these hard times.
I have the codes lines below which help me combine 8 different .xlsx into one, in different sheets. Attached you can find one of the 8, as an example. I want now to assign names each excel sheet according to the name of the weather station the data came from, in order to keep track easily. Eg., for the attached .xlsx the respective sheet name should be "Airport".
Could anyone help me? Thank you in advance!
filenames = {'C:\PhD\Projects\LIFE ASTI\C.3\WRF_main_TS_2015_ALL\WRF_Times_ALB+EMISS_FINAL2015\WRF_Thessaloniki_stations_2015\Evaluation_Output_2015(1st layer)\Airport\Monthly RH stats Airport 2015.xlsx','C:\PhD\Projects\LIFE ASTI\C.3\WRF_main_TS_2015_ALL\WRF_Times_ALB+EMISS_FINAL2015\WRF_Thessaloniki_stations_2015\Evaluation_Output_2015(1st layer)\Eptapurgio\Monthly RH stats Eptapurgio 2015.xlsx',...
'C:\PhD\Projects\LIFE ASTI\C.3\WRF_main_TS_2015_ALL\WRF_Times_ALB+EMISS_FINAL2015\WRF_Thessaloniki_stations_2015\Evaluation_Output_2015(1st layer)\Malakopi\Monthly RH stats Malakopi 2015.xlsx',...
'C:\PhD\Projects\LIFE ASTI\C.3\WRF_main_TS_2015_ALL\WRF_Times_ALB+EMISS_FINAL2015\WRF_Thessaloniki_stations_2015\Evaluation_Output_2015(1st layer)\Martiou\Monthly RH stats Martiou 2015.xlsx', 'C:\PhD\Projects\LIFE ASTI\C.3\WRF_main_TS_2015_ALL\WRF_Times_ALB+EMISS_FINAL2015\WRF_Thessaloniki_stations_2015\Evaluation_Output_2015(1st layer)\Paparrigopoulou\Monthly RH stats Paparrigopoulou 2015.xlsx',...
'C:\PhD\Projects\LIFE ASTI\C.3\WRF_main_TS_2015_ALL\WRF_Times_ALB+EMISS_FINAL2015\WRF_Thessaloniki_stations_2015\Evaluation_Output_2015(1st layer)\Parko\Monthly RH stats Parko 2015.xlsx', 'C:\PhD\Projects\LIFE ASTI\C.3\WRF_main_TS_2015_ALL\WRF_Times_ALB+EMISS_FINAL2015\WRF_Thessaloniki_stations_2015\Evaluation_Output_2015(1st layer)\Pedio Arews\Monthly RH stats Pedio Arews 2015.xlsx',...
'C:\PhD\Projects\LIFE ASTI\C.3\WRF_main_TS_2015_ALL\WRF_Times_ALB+EMISS_FINAL2015\WRF_Thessaloniki_stations_2015\Evaluation_Output_2015(1st layer)\Rooftop Zanis\Monthly RH stats Rooftop Zanis 2015.xlsx'};
outfile = [output_path,'\','Monthly RH stats All stations 2015.xlsx'];
for fidx = 1:numel(filenames),
fTable = readtable(filenames{fidx});
writetable(fTable,outfile,'Sheet',fidx);
end

Akzeptierte Antwort

Ameer Hamza
Ameer Hamza am 19 Mär. 2020
Bearbeitet: Ameer Hamza am 20 Mär. 2020
You can also specify the name of the sheet to the writetable function instead of the sheet number.
First extract the name of weather station from filenames
[names, ~] = regexp(filenames, 'stats\s([\sa-zA-Z]*)\s2015\.xlsx', 'tokens', 'match');
names = string(names);
This regular expression will work as long as the filename end with 2015.xlsx. Then use these names in writetables
for fidx = 1:numel(filenames),
fTable = readtable(filenames{fidx});
writetable(fTable,outfile,'Sheet',names(fidx));
end
  9 Kommentare
Stephen23
Stephen23 am 20 Mär. 2020
Bearbeitet: Stephen23 am 20 Mär. 2020
Note that in a regular expression the period character matches any character. If you want to match a literal period character, then it must be escaped:
'stats\s([\sa-zA-Z]*)\s2015\.csv'
% ^^ actually matches period character.
In this situation it is unlikely to make much difference, but it is worth knowing.
Ameer Hamza
Ameer Hamza am 20 Mär. 2020
Stephen, thanks for pointing out. I was a bit sloppy there.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by