How to display cfit data on plot

53 views (last 30 days)
Apurba Paul
Apurba Paul on 12 Aug 2020
Answered: Serhii Tetora on 12 Aug 2020
Is there a way to annote the fitting parameters with 95% coefficient into the plot. I am trying to do curve fit some data in a loop and want to displya the fitting results directly in the plot, may in some textbox. For example I am doing some curvefitting in the following code
%Generate data
%curve fitting
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.StartPoint =[1,1,];
%showing plot
%display fit results
Which generates plot like this
and the fitting results are displayed in the command window in the following way
General model:
fitresults(x) = a*exp(-x/b)
Coefficients (with 95% confidence bounds):
a = 2.031 (2.015, 2.047)
b = 5.329 (5.258, 5.4)
I want to display the fitting results inside the plot something like this
Is it possible to do using script.

Answers (1)

Serhii Tetora
Serhii Tetora on 12 Aug 2020
coeffs = coeffnames(fitresults);
coeffvals= coeffvalues(fitresults);
ci = confint(fitresults,0.95);
str1 = sprintf('\n %s = %0.3f (%0.3f %0.3f)',coeffs{1},coeffvals(1),ci(:,1));
str2 = sprintf('\n %s = %0.3f (%0.3f %0.3f)',coeffs{2},coeffvals(2),ci(:,2));
annotation('textbox',[.4 .5 .5 .2],'String',['Coefficients (with 95% confidence bounds): ', str1, str2]);

Community Treasure Hunt

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

Start Hunting!