Transferring Linear Models to excel/csv

2 views (last 30 days)
I am running multiple linear regressions with one X variable and 49 Y's. So I now have 49 different linear models (which I put into an array thoguh that isn't of much help in visually consolidating them). I want to put all the linear models into an xls/csv sheet, organized by variable, but the overly complicated structure of the Linear Model object has made it impossible for me to figure out a way to do so. My final resort may be manually building a table with each variable vs lm parameter but that method comes with its own snags, and there has got to be a more efficient way to do this. I also used diary but it's a very poor substitute (as the file in txt and the formatting is illegible). Help!
diary on
for i=2:50
XY = DataFinal{:,[1,i]};
%removing rows with NaN values in Y variable all columns have some missing data
XY(any(isnan(XY), 2), :) = [];
%creating linear model
Fit = fitlm(XY(:,1),XY(:,2))
%Adding significant pValue Y-variables to array pSig
if Fit.Coefficients.pValue(2) <= 0.05
%Adding fit model to array
Fitc{1,i-1} = Fit;
Sigs = [pSig,pValSig];
TSigs = array2table([pSig,pValSig], 'VariableNames',{'Significant Variables', 'p Values'})
diary off

Accepted Answer

Abdolkarim Mohammadi
Abdolkarim Mohammadi on 15 Aug 2020
It's better to first design a 49x1 cell array and write the outputs of each experiment to the corresponding element of the array, and then iterate over the entire 49x1 cell array to write the contents of each cell to Excel using writecell. You should increment the Range in writecell by a constant number which is essentially larger than the height of each table.
Kanak Gupta
Kanak Gupta on 17 Aug 2020
Oohh, I get it now. Thank you!!

Sign in to comment.

More Answers (0)

Community Treasure Hunt


Start Hunting!