Assign names to excel sheets with respet to the original filenames?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Daphne PARLIARI
le 19 Mar 2020
Commenté : Ameer Hamza
le 20 Mar 2020
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
0 commentaires
Réponse acceptée
Ameer Hamza
le 19 Mar 2020
Modifié(e) : Ameer Hamza
le 20 Mar 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 commentaires
Stephen23
le 20 Mar 2020
Modifié(e) : Stephen23
le 20 Mar 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.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Spreadsheets dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!