Save results on different sheets in excel

조회 수: 21 (최근 30일)
Aristi Karavi
Aristi Karavi 2021년 7월 8일
답변: Ankit 2021년 7월 12일
Hi,
I created an excel file named 'results' and then made different sheets. Now I don't know how to save the results in every sheet while for loop runs.
% Connect to Excel
Excel = actxserver('excel.application');
results = 'results.xlsx'
% Get Workbook object
WB = Excel.Workbooks.Open(fullfile(pwd, 'results.xlsx'), 0, false);
% Get Worksheets object
WS = WB.Worksheets;
k = 1
for s_rate = 1.1:0.1:1.5 % 5 different values
% Add a sheet after the last sheet
WS.Add([], WS.Item(WS.Count));
% Name the Sheets
while k <= 5
WB.Sheets.Item(k).Name
for r = 0.000000010:0.0000000010:0.00000010 % 100 different values
J = J_if(N0,v,b,C0,C,Qd,T,k,a,x,s_rate); % The 2-D nucleation rate (per unit time per unit area)
dh_dt = J*pi*(r^2)*a; % wire growth rate
% Add the results
writetable(k, results, 'Sheet' ,k, 'Range' ,'C2')
end
end
k = k + 1;
% Save
WB.Save();
end
%Quit Excel
Excel.Quit()
  댓글 수: 2
Ankit
Ankit 2021년 7월 9일
your first input to the writetable command should be in table format.
What you are trying to save in excel sheet?
Aristi Karavi
Aristi Karavi 2021년 7월 9일
I need 5 different sheets (extracted from 1st for loop (for s_rate)) and into them I need to save the values that extracted from the 2nd for loop ( loop for r).

댓글을 달려면 로그인하십시오.

답변 (2개)

Cris LaPierre
Cris LaPierre 2021년 7월 9일
See the Write Table to Specific Sheet and Range in Spreadsheet example on the writetable documenation page.

Ankit
Ankit 2021년 7월 12일
You can try this example and adapt to your requirement. first "for loop" you can use for creating 5 different sheets as mentioned by you. And second loop you can use for saving the values extracted from the inner loop.
results = 'results.xlsx';
for k = 1:1:2
res = table('Size',[1 4],'VariableNames',{'r','J','a','dh_dt'},'VariableTypes',{'double','double','double','double'});
for r = 0:1:5 % 100 different values
J = 1;
a = 2;
dh_dt = J*pi*(r^2)*a; % wire growth rate
% Add the results
res_new = table(r,J,a,dh_dt,'VariableNames',{'r','J','a','dh_dt'});
res = [res;res_new];
writetable(res,results,'Sheet',k)
end
end
Warning: Added specified worksheet.

카테고리

Help CenterFile Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by