Import mutilple excel files and save then individually as matlab tables, and finally synchronize the timetables

7 次查看(过去 30 天)
I am considering to import mutiple excel files using matlab, and these files will be saved individually as tables with the name corresponding to their excel files.
For example, excel file aberdeenshire_00150_aboyne-no-2.xlsx will be imported to matlab and save as aberdeenshire_00150_aboyne-no-2.mat; excel file aberdeenshire_00161_dyce.xlsx will be imported to matlab and save as aberdeenshire_00161_dyce.mat; excel file; antrim_01448_portglenone.xlsx will be imported to matlab and save as antrim_01448_portglenone.mat.
I have the following code but it save all the excel files in a single matlab table, could you teach me how to fix the code so that these files will be saved as table individually with the corresponding name?
Cheers!
source_dir = 'C:\Wind_Data_Rows_Deleted_2023\test';
dest_dir = 'C:\Wind_Forecast_Matlab';
source_files = dir(fullfile(source_dir, '*.xlsx'));
for i = 1:length(source_files)
data {i} = readtable(fullfile(source_dir, source_files(i).name));
end

采纳的回答

Voss
Voss 2024-9-4
编辑:Voss 2024-9-4
% specify source and destination directories
source_dir = 'C:\Wind_Data_Rows_Deleted_2023\test';
dest_dir = 'C:\Wind_Forecast_Matlab';
% get info about the relevant xlsx files,
% and construct xlsx full-path file names
F = dir(fullfile(source_dir, '*.xlsx'));
fn_xlsx = fullfile({F.folder},{F.name});
% construct the mat-file full-path file names
[~,fn] = fileparts(fn_xlsx);
fn_mat = fullfile(dest_dir,strcat(fn,'.mat'));
% if dest_dir doesn't already exist, create it
if ~isdir(dest_dir)
mkdir(dest_dir);
end
% read each xlsx file, and save each mat file
for ii = 1:numel(fn_xlsx)
T = readtable(fn_xlsx{ii});
save(fn_mat{ii},'T');
end
% check the mat file results
S = load(fn_mat{1});
S.T
ans = 8759x24 table
ob_end_time id_type id ob_hour_count met_domain_name version_num src_id rec_st_ind mean_wind_dir mean_wind_speed max_gust_dir max_gust_speed max_gust_ctime mean_wind_dir_q mean_wind_speed_q max_gust_dir_q max_gust_speed_q max_gust_ctime_q mean_wind_dir_j mean_wind_speed_j max_gust_dir_j max_gust_speed_j meto_stmp_time midas_stmp_etime ____________________ ________ _________ _____________ _______________ ___________ ______ __________ _____________ _______________ ____________ ______________ ______________ _______________ _________________ ______________ ________________ ________________ _______________ _________________ ______________ ________________ ____________________ ________________ 01-Jan-2023 00:00:00 {'WIND'} 1.241e+05 1 {'HCM'} 1 150 1011 300 3 310 5 2305 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 00:06:00 0 01-Jan-2023 00:59:59 {'WIND'} 1.241e+05 1 {'HCM'} 1 150 1011 290 3 290 5 30 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 01:03:59 0 01-Jan-2023 02:00:00 {'WIND'} 1.241e+05 1 {'HCM'} 1 150 1011 280 3 240 4 145 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 02:04:00 0 01-Jan-2023 03:00:00 {'WIND'} 1.241e+05 1 {'HCM'} 1 150 1011 270 2 260 4 150 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 03:03:00 0 01-Jan-2023 03:59:59 {'WIND'} 1.241e+05 1 {'HCM'} 1 150 1011 270 3 260 4 312 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 04:03:00 0 01-Jan-2023 05:00:00 {'WIND'} 1.241e+05 1 {'HCM'} 1 150 1011 260 3 250 4 442 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 05:03:00 0 01-Jan-2023 06:00:00 {'WIND'} 1.241e+05 1 {'HCM'} 1 150 1011 250 2 250 3 518 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 06:05:00 0 01-Jan-2023 06:59:59 {'WIND'} 1.241e+05 1 {'HCM'} 1 150 1011 260 2 260 2 628 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 07:03:59 0 01-Jan-2023 08:00:00 {'WIND'} 1.241e+05 1 {'HCM'} 1 150 1011 270 2 260 4 712 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 08:04:00 0 01-Jan-2023 09:00:00 {'WIND'} 1.241e+05 1 {'HCM'} 1 150 1011 280 1 270 2 757 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 09:06:00 0 01-Jan-2023 09:59:59 {'WIND'} 1.241e+05 1 {'HCM'} 1 150 1011 290 2 260 4 947 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 10:03:00 0 01-Jan-2023 11:00:00 {'WIND'} 1.241e+05 1 {'HCM'} 1 150 1011 310 2 280 4 951 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 11:03:00 0 01-Jan-2023 12:00:00 {'WIND'} 1.241e+05 1 {'HCM'} 1 150 1011 290 2 300 3 1144 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 12:03:00 0 01-Jan-2023 12:59:59 {'WIND'} 1.241e+05 1 {'HCM'} 1 150 1011 260 1 300 3 1155 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 13:03:59 0 01-Jan-2023 14:00:00 {'WIND'} 1.241e+05 1 {'HCM'} 1 150 1011 290 2 290 4 1324 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 14:04:00 0 01-Jan-2023 15:00:00 {'WIND'} 1.241e+05 1 {'HCM'} 1 150 1011 280 2 310 3 1427 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 15:03:00 0
S = load(fn_mat{2});
S.T
ans = 8760x24 table
ob_end_time id_type id ob_hour_count met_domain_name version_num src_id rec_st_ind mean_wind_dir mean_wind_speed max_gust_dir max_gust_speed max_gust_ctime mean_wind_dir_q mean_wind_speed_q max_gust_dir_q max_gust_speed_q max_gust_ctime_q mean_wind_dir_j mean_wind_speed_j max_gust_dir_j max_gust_speed_j meto_stmp_time midas_stmp_etime ____________________ ________ _________ _____________ _______________ ___________ ______ __________ _____________ _______________ ____________ ______________ ______________ _______________ _________________ ______________ ________________ ________________ _______________ _________________ ______________ ________________ ____________________ ________________ 01-Jan-2023 00:00:00 {'WIND'} 1.273e+05 1 {'HCM'} 1 161 1011 340 5 180 9 2332 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 00:06:00 0 01-Jan-2023 00:59:59 {'WIND'} 1.273e+05 1 {'HCM'} 1 161 1011 340 6 340 9 2 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 01:03:59 0 01-Jan-2023 02:00:00 {'WIND'} 1.273e+05 1 {'HCM'} 1 161 1011 330 3 320 6 115 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 02:04:00 0 01-Jan-2023 03:00:00 {'WIND'} 1.273e+05 1 {'HCM'} 1 161 1011 290 3 10 7 238 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 03:03:00 0 01-Jan-2023 03:59:59 {'WIND'} 1.273e+05 1 {'HCM'} 1 161 1011 350 5 330 10 317 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 04:03:59 0 01-Jan-2023 05:00:00 {'WIND'} 1.273e+05 1 {'HCM'} 1 161 1011 10 3 70 6 445 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 05:03:00 0 01-Jan-2023 06:00:00 {'WIND'} 1.273e+05 1 {'HCM'} 1 161 1011 360 4 60 6 456 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 06:05:00 0 01-Jan-2023 06:59:59 {'WIND'} 1.273e+05 1 {'HCM'} 1 161 1011 340 3 320 6 556 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 07:03:59 0 01-Jan-2023 08:00:00 {'WIND'} 1.273e+05 1 {'HCM'} 1 161 1011 340 3 330 7 707 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 08:04:00 0 01-Jan-2023 09:00:00 {'WIND'} 1.273e+05 1 {'HCM'} 1 161 1011 270 2 50 4 844 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 09:06:00 0 01-Jan-2023 09:59:59 {'WIND'} 1.273e+05 1 {'HCM'} 1 161 1011 350 1 350 2 938 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 10:03:00 0 01-Jan-2023 11:00:00 {'WIND'} 1.273e+05 1 {'HCM'} 1 161 1011 220 2 180 5 956 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 11:03:00 0 01-Jan-2023 12:00:00 {'WIND'} 1.273e+05 1 {'HCM'} 1 161 1011 170 2 220 4 1052 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 12:03:00 0 01-Jan-2023 12:59:59 {'WIND'} 1.273e+05 1 {'HCM'} 1 161 1011 340 3 340 4 1242 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 13:03:59 0 01-Jan-2023 14:00:00 {'WIND'} 1.273e+05 1 {'HCM'} 1 161 1011 330 6 320 9 1326 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 14:04:00 0 01-Jan-2023 15:00:00 {'WIND'} 1.273e+05 1 {'HCM'} 1 161 1011 330 5 330 7 1355 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 15:03:00 0
S = load(fn_mat{3});
S.T
ans = 8705x24 table
ob_end_time id_type id ob_hour_count met_domain_name version_num src_id rec_st_ind mean_wind_dir mean_wind_speed max_gust_dir max_gust_speed max_gust_ctime mean_wind_dir_q mean_wind_speed_q max_gust_dir_q max_gust_speed_q max_gust_ctime_q mean_wind_dir_j mean_wind_speed_j max_gust_dir_j max_gust_speed_j meto_stmp_time midas_stmp_etime ____________________ ________ ________ _____________ _______________ ___________ ______ __________ _____________ _______________ ____________ ______________ ______________ _______________ _________________ ______________ ________________ ________________ _______________ _________________ ______________ ________________ ____________________ ________________ 01-Jan-2023 00:00:00 {'WIND'} 9.13e+05 1 {'HCM'} 1 1448 1011 330 3 70 9 2251 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 00:06:00 0 01-Jan-2023 00:59:59 {'WIND'} 9.13e+05 1 {'HCM'} 1 1448 1011 320 3 340 5 2359 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 01:03:59 0 01-Jan-2023 02:00:00 {'WIND'} 9.13e+05 1 {'HCM'} 1 1448 1011 330 3 330 5 131 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 02:04:00 0 01-Jan-2023 03:00:00 {'WIND'} 9.13e+05 1 {'HCM'} 1 1448 1011 340 2 340 4 210 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 03:04:00 0 01-Jan-2023 03:59:59 {'WIND'} 9.13e+05 1 {'HCM'} 1 1448 1011 330 3 340 4 330 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 04:03:59 0 01-Jan-2023 05:00:00 {'WIND'} 9.13e+05 1 {'HCM'} 1 1448 1011 310 2 330 4 353 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 05:04:00 0 01-Jan-2023 06:00:00 {'WIND'} 9.13e+05 1 {'HCM'} 1 1448 1011 270 2 270 4 533 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 06:06:00 0 01-Jan-2023 06:59:59 {'WIND'} 9.13e+05 1 {'HCM'} 1 1448 1011 250 2 300 4 551 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 07:03:59 0 01-Jan-2023 08:00:00 {'WIND'} 9.13e+05 1 {'HCM'} 1 1448 1011 220 2 230 3 720 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 08:04:00 0 01-Jan-2023 09:00:00 {'WIND'} 9.13e+05 1 {'HCM'} 1 1448 1011 180 3 190 5 806 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 09:06:00 0 01-Jan-2023 09:59:59 {'WIND'} 9.13e+05 1 {'HCM'} 1 1448 1011 170 1 130 3 942 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 10:03:59 0 01-Jan-2023 11:00:00 {'WIND'} 9.13e+05 1 {'HCM'} 1 1448 1011 150 1 120 2 952 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 11:04:00 0 01-Jan-2023 12:00:00 {'WIND'} 9.13e+05 1 {'HCM'} 1 1448 1011 280 1 270 2 1135 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 12:04:00 0 01-Jan-2023 12:59:59 {'WIND'} 9.13e+05 1 {'HCM'} 1 1448 1011 270 1 240 2 1211 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 13:03:59 0 01-Jan-2023 14:00:00 {'WIND'} 9.13e+05 1 {'HCM'} 1 1448 1011 290 2 310 6 1345 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 14:04:00 0 01-Jan-2023 15:00:00 {'WIND'} 9.13e+05 1 {'HCM'} 1 1448 1011 260 2 300 4 1350 6 6 6 6 6 {'NA'} {'NA'} {'NA'} {'NA'} 01-Jan-2023 15:04:00 0
  12 个评论

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by