MATLAB: How to extract the output equation from a cfit object in Curve Fitting Toolbox 2.0 (R2009a)

cfitcurveequationfittingMATLABoutput

I used the curve fitting tool to generate MATLAB code for the fitting in Curve Fitting Toolbox 2.0 (R2009a). This code contains an object called 'cf_'. I would like to extract the fitted equation from this object, with the fitted values of the parameters? For example, I want to label plots with the equation.

Best Answer

There is no direct way to obtain the equation with the substituted parameter values from the 'cfit' object in Curve Fitting Toolbox 2.0 (R2009a). However, as a workaround, you can generate a string representation of the curve from a 'cfit' object, 'cf':
eq = formula(cf); %Formula of fitted equation
parameters = coeffnames(cf); %All the parameter names
values = coeffvalues(cf); %All the parameter values
for idx = 1:numel(parameters)
param = parameters{idx};
l = length(param);
loc = regexp(eq, param); %Location of the parameter within the string
while ~isempty(loc)
%Substitute parameter value
eq = [eq(1:loc-1) num2str(values(idx)) eq(loc+l:end)];
loc = regexp(eq, param);
end
end