MATLAB: How can i include the fitted model and goodness of fit into the regression analysis plot

curve fittingfigureslinear regression

Hello everyone
i have some x,y data. i have used the "Curve Fitting" app in matlab to carry out a simple linear regression analysis. it worked wonderfully.
i have quite a few data sets i would like to fit so i asked Matlab to generate Matlab code. But the matlab code that was generated only give the plot of the regression and the plot of the residuals. The model (ax+b) aswell as R^2 and adjusted R^2 is not shown anywhere. the code is shown below
%%Regression analysis
[xData, yData] = prepareCurveData(A(1,start:end),M(1,start:end));
% Set up fittype and options.
ft = fittype( 'poly1' ); % Linear polynomial curve, = ax+b
opts = fitoptions( ft );
opts.Lower = [-Inf -Inf];
opts.Upper = [Inf Inf];
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Create a figure for the plots.
figure( 'Name', 'Linear Regression' );
title('Linear Regression')
% Plot fit with data.
subplot( 2, 1, 1 );
h = plot( fitresult, xData, yData );
legend( h, 'Data', 'Linear Regression', 'Location', 'NorthEast' );
% Label axes

xlabel( 'x' );
ylabel( 'y' );
grid on
% Plot residuals.
subplot( 2, 1, 2 );
h = plot( fitresult, xData, yData, 'residuals' );
legend( h, 'Linear Regression - residuals', 'Zero Line', 'Location', 'NorthEast' );
% Label axes
xlabel( 'x' );
ylabel( 'Residuals' );
grid on
Both the model and the goodness of fit is saved as variables, "fitresult" and "gof" respectivly. but i do not know how to display them together/inside the figure. i can of course make the figure and then afterwards use e.g. disp(gof) to retrieve the goodness of fit. but then i have to put them together in another program before i can actually use the figure
i have the statistics toolbox installed in case that is relevant

Best Answer

Try something like this:
load census
[fitresult,gof] = fit(cdate,pop,'poly2')
plot(fitresult,cdate,pop)
text(1800,100,sprintf('Rsq = %g\nAdj = %g',gof.rsquare,gof.adjrsquare))