How to export summary results of the LinearModel as text to an excel file?

12 views (last 30 days)
Hello,
I run the x=LinearModel.fit(ds) and create 'x' model object with lots of details in it. Now, typing x in a command window displays summary results of the model fit. I want that text summary displayed to be written in an excel file. I can work with the xlswrite, just cannot retrieve the summary text you see in a command window. How do I retrieve this text? I am copying an example of the summary text I am talking about. Thanks!
%%%
x=LineatModel.fit(ds)
disp(x)
ans =
Linear regression model: y ~ [Linear formula with 11 terms in 10 predictors]
Estimated Coefficients:
Estimate SE tStat
(Intercept) 1.676 0.013365 125.41
x1 0.0069673 0.0011407 6.1081
x2 0.0058617 0.0011867 4.9393
x3 0.0066048 0.0011257 5.8671
x4 0.010384 0.0011523 9.0109
x5 0.0081183 0.0011798 6.8813
x6 0.0058441 0.0011711 4.9901
x7 0.0089474 0.0011568 7.7348
x8 0.011345 0.001145 9.9088
x9 0.0078164 0.0011485 6.8059
x10 0.0017628 0.0011648 1.5135
pValue
(Intercept) 0
x1 1.4477e-09
x2 9.1991e-07
x3 6.0446e-09
x4 1.0325e-18
x5 1.0516e-11
x6 7.1276e-07
x7 2.5446e-14
x8 3.9344e-22
x9 1.7387e-11
x10 0.13048
Number of observations: 1000,
Error degrees of freedom: 989
Root Mean Squared Error: 0.251,
R-squared: 0.323,
Adjusted R-Squared 0.316,
F-statistic vs. constant model: 47.3,
p-value = 3.71e-77

Accepted Answer

David Holtschlag
David Holtschlag on 6 Oct 2014
You've probably moved on or figured it out, but I had the same question and found that the following command works fine for storing the model mdl output in a char string:
T = evalc('disp(mdl)');
Then,
fid = fopen(fullfile(pName,fName),'wt'); fprintf(fid,'%s',mdlOutput); fclose(fid);
I found the answer from a post on the use of evalc. Sorry, I don't have the URL.

More Answers (1)

Marina Fernandez
Marina Fernandez on 21 Sep 2022
Another option is to convert the model to text, separate it by rows and convert that to a cell in order to write the results of the model in excel format:
text_model = evalc('disp(model)');
split_text_model = split(text_model,char(10));
xlswrite(excel_path,cellstr(split_text_model),'results','A1');

Community Treasure Hunt

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

Start Hunting!