How can I write multiple sets of data from MATLAB to one sheet in excel?

1 回表示 (過去 30 日間)
Alexandra Brian
Alexandra Brian 2016 年 10 月 26 日
コメント済み: Chaya N 2016 年 10 月 28 日
I'm running a linear regression test in MATLAB from data I have in excel.
A = xlsread('excel_report.xlsx', sheet, 'ColumnA:ColumnA');
B = xlsread('excel_report.xlsx', sheet, 'ColumnB:ColumnB');
After using the xlsread built-in function to download the data I ran a linear regression test using fitlm:
md2 = fitlm(A,B);
I then pulled data from the fitlm:
coeff2 = md2.Coefficients;
And added this to same excel workbook I originally pulled the data from:
writetable(coeff2, 'excel_report.xlsx', 'Sheet', 2);
My problem is I'd like to add more variables from fitlm to the excel_report.xlsx's corresponding sheet.
Specifically the Rsquared value and the number observations.

採用された回答

Chaya N
Chaya N 2016 年 10 月 26 日
編集済み: Chaya N 2016 年 10 月 26 日
You could simply call the same function a couple more times to write your required variables into the same sheet.
I would also suggest using the 'Range' option of the function to specify the cell(s) you would be writing your data into. Try
writetable(md2.Rsquared.Ordinary, 'excel_report.xlsx', 'Sheet', 2, 'Range', 'B2');
writetable(md2.Rsquared.Adjusted, 'excel_report.xlsx', 'Sheet', 2, 'Range', 'B4');
writetable(md2.NumObservations, 'excel_report.xlsx', 'Sheet', 2, 'Range', 'B6');
writetable(md2.Coefficients, 'excel_report.xlsx', 'Sheet', 2, 'Range', 'D2');
  2 件のコメント
Alexandra Brian
Alexandra Brian 2016 年 10 月 27 日
The following error occurred when I added the range:
Undefined function 'write' for input arguments of type 'double'.
Error in writetable (line 116)
write(a,filename,varargin{:})
Error in correlAnalysis (line 47)
writetable(md2.Rsquared.Ordinary, 'RB4Indicators_Report.xlsx', 'Sheet', 2, 'Range', 'B2');
Chaya N
Chaya N 2016 年 10 月 28 日
Please use the xlswrite() command instead of writetable(). Note the change in the order of the input arguments between the two functions.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAnalysis of Variance and Covariance についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by