Saving tables as separate Excel file with different file names
2 ビュー (過去 30 日間)
古いコメントを表示
Hi all. I'm relatively new here. Just like to ask if there is a way to save tables into different Excel files with different file names?
By running this code in the uploaded 'sample.mat' file:
for hh = [1:4]
Group = table2array(sample);
Group_idx = Group(:,5) == hh;
Final_Group(hh).iter = array2table(Group(Group_idx,:));
Final_Group(hh).iter.Properties.VariableNames = {'Customer','x','y','d','X_cluster'}
end
I would be able to separate the initial 50x5 table into a struct containing 4 different sized tables grouped according to the Column 5, 'X_cluster'. (See uploaded 'Final_Group.mat')
My questions are the following:
1.) Is there a way to save the 4 different tables within the Final_Group struct into 4 separate Excel files having different file names? (e.g. Final_Group(1).iter will be saved as Results1.xlsx, Final_Group(2).iter will be saved as Results2.xlsx, etc...)
2.) If #1 is possible, can those Results1.xlsx, Results2.xlsx, etc... files be loaded back into Matlab automically? (i.e. NOT loaded one-by-one manually using their unique file name)
Any help is greatly appreciated.
Thank you.
0 件のコメント
回答 (1 件)
Mathieu NOE
2020 年 11 月 29 日
hello ,
this is a code that will do the job and also vice versa
but IMHO, it's better so save your different data in one excel file with multiple sheets rather than multiple excel files
of course you are free to modify the code
load('sample.mat');
Group = table2array(sample);
VariableNames = {'Customer','x','y','d','X_cluster'};
filename_out = 'test.xlsx';
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% part 1 : Saving Data to excel - multiple sheets
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for hh = [1:4]
Group_idx = find(Group(:,5) == hh);
Final_Group{hh} = Group(Group_idx,:);
% export to excel
writecell(VariableNames,filename_out,"Sheet" ,hh,"Range",'A1'); % save column headers in first line
writematrix(Final_Group{hh},filename_out,"Sheet" ,hh,"Range",'A2'); % save data (cell) to excel file
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% part 2 : Importing Data from excel across multiple sheets and filenames.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[~,sheet_name]=xlsfinfo(filename_out);
for k=1:numel(sheet_name)
[~,~,newdata{k}]=xlsread(filename_out,sheet_name{k});
end
% Now all your data are in the variable data.
5 件のコメント
Mathieu NOE
2020 年 11 月 30 日
hi
I understand the input data are the same , but the final code is different , or ?
that's what I am interested to see
参考
カテゴリ
Help Center および File Exchange で Spreadsheets についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!