Non-numeric data type using fit function
Show older comments
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 Comments
Adam Danz
on 7 Aug 2019
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
Chris Angeloni
on 7 Aug 2019
Adam Danz
on 7 Aug 2019
Any way you can attach a mat file with these 3 variables?
- x,
- y,
- gaussEqn
Chris Angeloni
on 7 Aug 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.

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?
Chris Angeloni
on 8 Aug 2019
Sean de Wolski
on 8 Aug 2019
Perhaps there's another function overloaded? Perhaps class(), isa()...
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
Chris Angeloni
on 9 Aug 2019
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.
Sean de Wolski
on 13 Aug 2019
If you want to take a big hammer approach, run
restoredefaultpath;rehash toolboxcache
This will reliably get rid of all of the extra stuff.
Answers (1)
Chris Angeloni
on 13 Aug 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 Comment
Adam Danz
on 13 Aug 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.
Categories
Find more on Spline Postprocessing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!