Asked by Kai
on 26 Jun 2013

Hello,

I stored a cfit obj to hard disc. It looks like:

General model:

ans(xd) = off+amp./(1+exp(-a.*(xd-b))) Coefficients (with 95% confidence bounds): off = -4.047 (-4.054, -4.041) amp = 8.179 (8.169, 8.189) a = 4.053 (4.034, 4.073) b = 2.271 (2.27, 2.273)

In the process of creating the obj I was able to plot it and to calculate the derivatives.

Now I want to come back to these data and tried:

>>plot(cfObj)

but receive:

Error using cfit/plot (line 111) Error evaluating CFIT function: Error while trying to evaluate CFIT model: obj:

Error while trying to evaluate FITTYPE function obj:

Argument must contain a string or function_handle.

For:

>>differntiate(cfObj, x)

I receive:

Error using cfit/feval (line 31) Error while trying to evaluate CFIT model: fitobj:

Error while trying to evaluate FITTYPE function obj:

Argument must contain a string or function_handle.

Error in cfit/differentiate (line 33) f1 = feval(fitobj,x+ms);

Can anyone please help out with that? Sure I can recreate the fit from the coefficients, but I think it is not meant to be like that.

Thanks,

Kai

Answer by Onno Broekmans
on 1 Jul 2013

Accepted answer

I can confirm this behavior (see code to reproduce at the bottom). Unfortunately, it also affects parallel processing: if you try to perform a large number of fits in parallel, using a 'parfor' loop, the resulting cfit objects are "broken" in the way described by Kai.

Unless I've missed something, I'd consider this a bug. I've filed a Service Request (ID: 1-NE8ATH) with TMW. I'll get back to this thread when I hear back.

As a workaround, you could try manually evaluting the fit's anonymous function, and using the coeffvalues() function to retrieve the coefficients from the cfit object (that **does** work fine).

- Onno

Code to reproduce the issue:

%% Generate some random data, and plot it

data_x = 1:50; data_y = (2.* data_x) + randn(size(data_x)) + 5;

figure; plot(data_x, data_y, '.b'); title('Original data');

%% Fit the data using a simple model model = @(A, B, x) A.*x + B;

ftype = fittype(model); fopt = fitoptions(ftype); fopt.StartPoint = [1 1];

f = fit(data_x(:), data_y(:), ftype, fopt);

%% Plot the fit figure; hold on; plot(data_x, data_y, '.b'); plot(f, '-r'); hold off; title('Fitted data');

%% Store cfit object on disk, and then retrieve it again save('fit.mat', 'f'); clear('f');

load('fit.mat');

figure; hold on; plot(data_x, data_y, '.b'); plot(f, '-r'); % <-- crash here: % Error using cfit/plot (line 113) % Error evaluating CFIT function: % Error while trying to evaluate CFIT model: obj: % Error while trying to evaluate FITTYPE function obj: % Argument must contain a string or function_handle.

hold off; title('Fitted data (fit loaded from disk)');

Kai
on 1 Jul 2013

Hi Onno,

thanks for the code. It exactly reproduces the error. Next time I will communicate with code as well ;) Adding:

try d1 = differentiate(f, data_x); catch err disp(err.message) end

on line 36 shows the other error s. above.

Also, thanks for filing the bug report, looking forward to the response. So long, I ll use the coefficients.

Best, Kai

Onno Broekmans
on 1 Aug 2013

I forgot to post back, but TMW confirmed the issue (early July), and said they had their developers working on it.

Jon Cherrie
on 21 Aug 2013

We have published a patch for this issue. See the bug report 968079 for details.

Answer by Andrei Bobrov
on 26 Jun 2013

Edited by Andrei Bobrov
on 26 Jun 2013

x = (0:.01:4)'; % y = -4 + 8./(1+exp(4.*(x - 2))) + .5*randn(numel(x),1); % your data

ftfun = fit(x,y,fittype(@(a,b,c,d,x)a + b./(1+exp(c.*(x - d)))),... 'Startpoint',[1 1 1 1])

dft = differentiate(ftfun,x);

plot(x,[y,ftfun(x),dft])

Kai
on 26 Jun 2013

Hi Andrei,

thanks for your answer. As I see it, you describe how to create a fit object and then use plot and differentiate. This is not really an answer to my question, because I already have the fit objects and know how to use the mentioned functions. The point is that directly after creation I could plot and differentiate them. But now, after a few days, I receive the errors shown above. Weird, I think. Or did I misunderstand your point?

Kai

Related Content

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
## 2 Comments

## Onno Broekmans (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/80285-cfit-object-doesn-t-work-with-plot-or-differentiate#comment_157753

Your fit results are definitely not useless. You can use coeffvalues(cfObj) to get the coefficient values from the fit manually, and then plot them using:

(Warning: untested code, so there might be minor syntax errors in there. I'm assuming your x data is in the variable 'x').

Note that this is what plot(cfObj,x) does internally, so the results are exactly the same :)

## Kai (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/80285-cfit-object-doesn-t-work-with-plot-or-differentiate#comment_157796

Right, that's what I will do. I was only a bit shocked not seeing it come out in the first place. The function I will use the fits in looks like:

So, I see the 'expr' statement growing a bit longer than before...