Non-numeric data type using fit function

This is driving me crazy, I'm just trying to fit a gaussian curve to some data using Matlab's fit function (r2017a).
gaussEqn = 'a*exp(-((x-b)/c)^2)+d'
f = fit(x,y,gaussEqn);
But I get this error:
Error using fit>iFit (line 340)
Invalid data type. Second argument must be numeric or logical.
Yet my variables are both numeric...
whos x
Name Size Bytes Class Attributes
x 101x1 808 double
and
whos y
Name Size Bytes Class Attributes
y 101x1 808 double
Whyyyyy is this happening? At this point, I'll use a different function to fit my data, but it doesn't make any sense (and the internal fitting function iFit is not accessible)
_____________________________
A little extra detail:
The x and y variables are time and the mean of a matrix of interest; x for instance:
fs = 1000;
w = .1;
t = (-w:1/fs:0) * 1000; x = t';
The y variable is a bit more complicated, it is the mean of an estimated neural filter, which is computed by a different function that I wrote:
STA = genSTA(find(spkV),NSr(:,:,stimI),w,fs);
whos STA
Name Size Bytes Class Attributes
STA 25x101 20200 double
y = mean(STA,1);
whos y
Name Size Bytes Class Attributes
y 101x1 808 double

12 commentaires

Let's test that you're using the correct fit() function.
What's the result of
which fit -all
When I run this, a list of fit functions that all belong to matlab or a matlab toolbox appear (below). Look for any custom functions named "fit" that may be interfering.
C:\Program Files\MATLAB\R2019a\toolbox\curvefit\curvefit\fit.p
C:\Program Files\MATLAB\R2019a\toolbox\stats\stats\@gmdistribution\fit.m % gmdistribution method
C:\Program Files\MATLAB\R2019a\toolbox\curvefit\curvefit\fit.m % Shadowed
seems fine:
which fit -all
/Applications/MATLAB_R2019a.app/toolbox/curvefit/curvefit/fit.p
/Applications/MATLAB_R2019a.app/toolbox/stats/stats/@gmdistribution/fit.m % gmdistribution method
/Applications/MATLAB_R2019a.app/toolbox/curvefit/curvefit/fit.m % Shadowed
Adam Danz
Adam Danz le 7 Août 2019
Any way you can attach a mat file with these 3 variables?
  • x,
  • y,
  • gaussEqn
Chris Angeloni
Chris Angeloni le 7 Août 2019
Sure, see attached
Adam Danz
Adam Danz le 7 Août 2019
Modifié(e) : Adam Danz le 7 Août 2019
Worked fine for me. Are you doing anything different than this (see the first two lines of code in the image below)?
I see from you previous comment that you're also using r2019a and do not have any additional fit() functions from what I have so that's puzzling.
I would double check that you're not doing anything different from what I did by clearing out your workspace, loading the file you attached, and executing fit(x,y,gaussEqn).
Another possibility is that your fit.m function has been altered. Click on the error message to open fit.m to line 340. This is what lines 340 & 341 look like on my fit.m.
190807 173934-Editor - C__Program Files_MATLAB_R2019a_toolbox_curvefit_curvefit_fit.m.jpg
Adam Danz
Adam Danz le 7 Août 2019
Modifié(e) : Adam Danz le 7 Août 2019
Also, I cannot recreate your error. "The 2nd argument must be numeric or logical" .
I've tried entering characters, a cell array of numbers, a cell array of characters, but I get different errors.
It really sounds like you're using a different function named fit(). Is there perhaps a local function named fit() within your code?
Ok, so it works when I restart matlab and run in a clean workspace:
f = fit(x,y,gaussEqn)
Warning: Start point not provided, choosing random start point.
> In curvefit.attention.Warning/throw (line 30)
In fit>iFit (line 307)
In fit (line 116)
f =
General model:
f(x) = a*exp(-((x-b)/c)^2)+d
Coefficients (with 95% confidence bounds):
a = 0.4627
b = 0.4246
c = 0.01791
d = -0.002797 (-0.006663, 0.00107)
and running which:
which fit -all
/Applications/MATLAB_R2019a.app/toolbox/curvefit/curvefit/fit.p
/Applications/MATLAB_R2019a.app/toolbox/stats/stats/@gmdistribution/fit.m % gmdistribution method
/Applications/MATLAB_R2019a.app/toolbox/curvefit/curvefit/fit.m % Shadowed
However, when I add some of my common code libraries, it fails:
addpath(genpath('~/chris-lab/code_general'));
addpath(genpath('./_plotting_functions'));
addpath(genpath('./_analysis_functions'));
load('fit_debug.mat')
gaussEqn = 'a*exp(-((x-b)/c)^2)+d';
f = fit(x,y,gaussEqn)
Warning: Start point not provided, choosing random start point.
> In curvefit.attention.Warning/throw (line 30)
In fit>iFit (line 307)
In fit (line 116)
Error using fit>iFit (line 348)
Invalid data type. Second argument must be numeric or logical.
Error in fit (line 116)
[fitobj, goodness, output, convmsg] = iFit( xdatain, ydatain, fittypeobj, ...
But I'm still confused, because which shows the same outcome as when it worked, and there is no local function called fit...
which fit -all
/Applications/MATLAB_R2019a.app/toolbox/curvefit/curvefit/fit.p
/Applications/MATLAB_R2019a.app/toolbox/stats/stats/@gmdistribution/fit.m % gmdistribution method
/Applications/MATLAB_R2019a.app/toolbox/curvefit/curvefit/fit.m % Shadowed
Sean de Wolski
Sean de Wolski le 8 Août 2019
Perhaps there's another function overloaded? Perhaps class(), isa()...
Adam Danz
Adam Danz le 8 Août 2019
Modifié(e) : Adam Danz le 9 Août 2019
Hmmm, open fit.m, go to line 116 where iFit() is called, and put a break on that line. Execute the code that would normally cause an error. Then tell us exactly what the inputs look like to iFit(). I'm not sure if that will lead anywhere but it might.
Also, regarding "Error using fit>iFit (line 348)", there's a comment in fit.m right before that line. This comment might suggest that there are NaN, Inf, or strings in the output in the 'try' block.
try
[xout,resnorm,res,exitflag,optoutput,activebounds,convmsg,J] = iNonLinearFit(...
model, start,xdata,wtdydata,lowerbnd,upperbnd, options,probparams, ...
separargs,weights, optimargs, nonlcoeffindex);
catch es
% Note: we can't use the ID from the exception as this comes from LSQNCOMMON;
% only the error message includes the Inf/NaN/complex strings. Therefore we need
% to look into the cause.
errorFcn.throw( iHandleFevalError( es ) ); % <------ line 348
return;
end
@Sean de Wolski: how can I tell if there is another function overloaded, besides using which? Because fit errors out, I don't have a fit object to check using class() or isa()?
fit.m is read only, but I made a copy and put a debug point, not sure how many of the inputs you want, but ydatain is the second argument to iFit, which is what is supposedly the culprit:
whos xdatain ydatain
Name Size Bytes Class Attributes
xdatain 101x1 808 double
ydatain 101x1 808 double
Adam Danz
Adam Danz le 9 Août 2019
Modifié(e) : Adam Danz le 17 Mar 2020
No need to make a copy of fit.m. Adding a break point does not require editing the code (see the link). Another advantage of adding a break point instead of copying the code is to ensure that fit.m is the exact function being invoked.
Anyway, when you executed the copied file, did it give you the same error? If xdatain and ydatain are both numeric, double, column vectors and you got that same error with those exact inputs, then I'm completely puzzled.
If you want to take a big hammer approach, run
restoredefaultpath;rehash toolboxcache
This will reliably get rid of all of the extra stuff.

Connectez-vous pour commenter.

Réponses (1)

Chris Angeloni
Chris Angeloni le 13 Août 2019

0 votes

I've long since been able to fit this data with another function, and I'm not going to put any more effort into trying to use fit.m. Based on the fact that loading my library creates the error, I'm gonna put it down to some conflict there... sorry guys, thanks for your help and comments!

1 commentaire

Adam Danz
Adam Danz le 13 Août 2019
Just something to keep in mind; fit() is a common function and even if you remember to avoid using it, you could run into a toolbox or other function that uses fit() in which case, you're back to square one.
I think it's worth the time to figure out what program is interfering with your call to fit(). It could cause more problems down the lane.

Connectez-vous pour commenter.

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by