MATLAB: Indexing error message that makes no sense with cfit

curve fitting

Hi y'all,
I'm trying to fit a polynomial to some data, and I keep on getting an indexing error message which makes no sense. Perhaps one of you can see the forest for the trees. Here's an example of the data:
r =
23.6406 2.8430
16.8598 1.9837
10.8369 1.2979
6.8227 1.0700
15.3950 1.8485
17.8152 2.5149
21.3562 4.4245
23.5249 2.7839
27.0158 1.6553
24.3364 3.1090
21.7304 2.3873
15.4503 1.3488
21.8857 1.9761
28.4438 8.2376
17.5644 7.2797
14.6695 4.7655
24.3061 1.9693
24.2177 3.8884
26.6174 2.3952
26.3180 8.7369
19.6436 5.7631
-4.3673 1.1343
1.9130 0.5210
9.9632 6.3485
16.6363 4.9399
14.4421 3.3374
but when I try and fit it I get an indexing error message which makes no sense:
f=fit(r(:, 1), r(:, 2),'poly2')
Subscript indices must either be real positive integers or logicals.
I don't see how this can be an indexing issue, as I can index to plot the full dataset:
figure, plot(r(:, 1), r(:, 2), '.')
Any ideas? I've even re-started matlab as it does sometimes get buggy when I've had it open for a while, but no dice.
Thanks!

Best Answer

My guess is that the error refers to fit, and MATLAB believes it’s an array, not a function. Do you have a previously assigned variable named ‘fit’ that’s ‘overshadowing’ the function name?
Run this from your Command Window or a script:
which fit -all
If the first line in the returned output is:
fit is a variable.
You’ve found the problem. The solution is to name the variable something else.