Assign names to excel sheets with respet to the original filenames?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Ameer Hamza
2020-3-19
编辑:Ameer Hamza
2020-3-20
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 个评论
Stephen23
2020-3-20
编辑:Stephen23
2020-3-20
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!