MATLAB: Obtaining slope from fitlm results

fitlmlinear regressionplotslopeStatistics and Machine Learning Toolbox

With results from fitlm, how does one disentangle the linear regression's slope? I'm simply trying to plot the regression over the data. (I expect this'll be an easy answer; thank you in advance.)

Best Answer

Oh, it's a tangled web TMW has weaved, fer shure...what with all the different mechanisms to do the same thing in different toolboxen plus the base product, and nary the doubled twain shall meet...
Anyhoo, fitlm in Statistics Toolbox returns an object of the Linear Model class. There's a page documenting properties and methods for it you can get to by clicking on the Output Arguments section link mdl or the 'See Also' section for more details.
In short, the coefficients are obtainable by referencing the field Estimate in the table returned by Coefficients property. So, given
lm=fitlm(x,y,'poly1'); % a linear model
betahat=lm.Coefficients.Estimate; % the coefficients
NB: These are case-sensitive names, btw...and must be spelled-out in their entirety, none of this "first n characters stuff" here (not that that's any different than for other dot addressing, just noting it).
BTW, the order of these are intercept first which is in direct conflict with the venerable polyfit/polyval pair.
To just evaluate the fit to draw the regression line, use predict method that will get the coefficients for you given just the model object and x vector.